xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_texture.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include <stdio.h>
29 
30 #include "st_context.h"
31 #include "st_format.h"
32 #include "st_texture.h"
33 #include "main/enums.h"
34 
35 #include "pipe/p_state.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
39 #include "util/format/u_format.h"
40 #include "util/u_rect.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "tgsi/tgsi_from_mesa.h"
44 
45 
46 #define DBG if(0) printf
47 
48 
49 /**
50  * Allocate a new pipe_resource object
51  * width0, height0, depth0 are the dimensions of the level 0 image
52  * (the highest resolution).  last_level indicates how many mipmap levels
53  * to allocate storage for.  For non-mipmapped textures, this will be zero.
54  */
55 struct pipe_resource *
st_texture_create(struct st_context * st,enum pipe_texture_target target,enum pipe_format format,GLuint last_level,GLuint width0,GLuint height0,GLuint depth0,GLuint layers,GLuint nr_samples,GLuint bind,bool sparse,uint32_t compression)56 st_texture_create(struct st_context *st,
57                   enum pipe_texture_target target,
58                   enum pipe_format format,
59                   GLuint last_level,
60                   GLuint width0,
61                   GLuint height0,
62                   GLuint depth0,
63                   GLuint layers,
64                   GLuint nr_samples,
65                   GLuint bind,
66                   bool sparse,
67                   uint32_t compression)
68 {
69    struct pipe_resource pt, *newtex;
70    struct pipe_screen *screen = st->screen;
71 
72    assert(target < PIPE_MAX_TEXTURE_TYPES);
73    assert(width0 > 0);
74    assert(height0 > 0);
75    assert(depth0 > 0);
76    if (target == PIPE_TEXTURE_CUBE)
77       assert(layers == 6);
78 
79    DBG("%s target %d format %s last_level %d\n", __func__,
80        (int) target, util_format_name(format), last_level);
81 
82    assert(format);
83    assert(screen->is_format_supported(screen, format, target, 0, 0,
84                                       PIPE_BIND_SAMPLER_VIEW));
85 
86    memset(&pt, 0, sizeof(pt));
87    pt.target = target;
88    pt.format = format;
89    pt.last_level = last_level;
90    pt.width0 = width0;
91    pt.height0 = height0;
92    pt.depth0 = depth0;
93    pt.array_size = layers;
94    pt.usage = PIPE_USAGE_DEFAULT;
95    pt.bind = bind;
96    /* only set this for OpenGL textures, not renderbuffers */
97    pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY;
98    pt.nr_samples = nr_samples;
99    pt.nr_storage_samples = nr_samples;
100    pt.compression_rate = compression;
101 
102    if (sparse)
103       pt.flags |= PIPE_RESOURCE_FLAG_SPARSE;
104 
105    newtex = screen->resource_create(screen, &pt);
106 
107    assert(!newtex || pipe_is_referenced(&newtex->reference));
108 
109    return newtex;
110 }
111 
112 
113 /**
114  * In OpenGL the number of 1D array texture layers is the "height" and
115  * the number of 2D array texture layers is the "depth".  In Gallium the
116  * number of layers in an array texture is a separate 'array_size' field.
117  * This function converts dimensions from the former to the later.
118  */
119 void
st_gl_texture_dims_to_pipe_dims(GLenum texture,unsigned widthIn,uint16_t heightIn,uint16_t depthIn,unsigned * widthOut,uint16_t * heightOut,uint16_t * depthOut,uint16_t * layersOut)120 st_gl_texture_dims_to_pipe_dims(GLenum texture,
121                                 unsigned widthIn,
122                                 uint16_t heightIn,
123                                 uint16_t depthIn,
124                                 unsigned *widthOut,
125                                 uint16_t *heightOut,
126                                 uint16_t *depthOut,
127                                 uint16_t *layersOut)
128 {
129    switch (texture) {
130    case GL_TEXTURE_1D:
131    case GL_PROXY_TEXTURE_1D:
132       assert(heightIn == 1);
133       assert(depthIn == 1);
134       *widthOut = widthIn;
135       *heightOut = 1;
136       *depthOut = 1;
137       *layersOut = 1;
138       break;
139    case GL_TEXTURE_1D_ARRAY:
140    case GL_PROXY_TEXTURE_1D_ARRAY:
141       assert(depthIn == 1);
142       *widthOut = widthIn;
143       *heightOut = 1;
144       *depthOut = 1;
145       *layersOut = heightIn;
146       break;
147    case GL_TEXTURE_2D:
148    case GL_PROXY_TEXTURE_2D:
149    case GL_TEXTURE_RECTANGLE:
150    case GL_PROXY_TEXTURE_RECTANGLE:
151    case GL_TEXTURE_EXTERNAL_OES:
152    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
153    case GL_TEXTURE_2D_MULTISAMPLE:
154       assert(depthIn == 1);
155       *widthOut = widthIn;
156       *heightOut = heightIn;
157       *depthOut = 1;
158       *layersOut = 1;
159       break;
160    case GL_TEXTURE_CUBE_MAP:
161    case GL_PROXY_TEXTURE_CUBE_MAP:
162    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
163    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
164    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
165    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
166    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
167    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
168       assert(depthIn == 1);
169       *widthOut = widthIn;
170       *heightOut = heightIn;
171       *depthOut = 1;
172       *layersOut = 6;
173       break;
174    case GL_TEXTURE_2D_ARRAY:
175    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
176    case GL_PROXY_TEXTURE_2D_ARRAY:
177    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
178       *widthOut = widthIn;
179       *heightOut = heightIn;
180       *depthOut = 1;
181       *layersOut = depthIn;
182       break;
183    case GL_TEXTURE_CUBE_MAP_ARRAY:
184    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
185       *widthOut = widthIn;
186       *heightOut = heightIn;
187       *depthOut = 1;
188       *layersOut = util_align_npot(depthIn, 6);
189       break;
190    default:
191       unreachable("Unexpected texture in st_gl_texture_dims_to_pipe_dims()");
192    case GL_TEXTURE_3D:
193    case GL_PROXY_TEXTURE_3D:
194       *widthOut = widthIn;
195       *heightOut = heightIn;
196       *depthOut = depthIn;
197       *layersOut = 1;
198       break;
199    }
200 }
201 
202 
203 /**
204  * Check if a texture image can be pulled into a unified mipmap texture.
205  */
206 GLboolean
st_texture_match_image(struct st_context * st,const struct pipe_resource * pt,const struct gl_texture_image * image)207 st_texture_match_image(struct st_context *st,
208                        const struct pipe_resource *pt,
209                        const struct gl_texture_image *image)
210 {
211    unsigned ptWidth;
212    uint16_t ptHeight, ptDepth, ptLayers;
213 
214    /* Images with borders are never pulled into mipmap textures.
215     */
216    if (image->Border)
217       return GL_FALSE;
218 
219    /* Check if this image's format matches the established texture's format.
220     */
221    if (st_mesa_format_to_pipe_format(st, image->TexFormat) != pt->format)
222       return GL_FALSE;
223 
224    st_gl_texture_dims_to_pipe_dims(image->TexObject->Target,
225                                    image->Width, image->Height, image->Depth,
226                                    &ptWidth, &ptHeight, &ptDepth, &ptLayers);
227 
228    /* Test if this image's size matches what's expected in the
229     * established texture.
230     */
231    if (ptWidth != u_minify(pt->width0, image->Level) ||
232        ptHeight != u_minify(pt->height0, image->Level) ||
233        ptDepth != u_minify(pt->depth0, image->Level) ||
234        ptLayers != pt->array_size)
235       return GL_FALSE;
236 
237    if (image->Level > pt->last_level)
238       return GL_FALSE;
239 
240    return GL_TRUE;
241 }
242 
243 void
st_texture_image_insert_transfer(struct gl_texture_image * stImage,unsigned index,struct pipe_transfer * transfer)244 st_texture_image_insert_transfer(struct gl_texture_image *stImage,
245                                  unsigned index,
246                                  struct pipe_transfer *transfer)
247 {
248    /* Enlarge the transfer array if it's not large enough. */
249    if (index >= stImage->num_transfers) {
250       unsigned new_size = index + 1;
251 
252       stImage->transfer = realloc(stImage->transfer,
253                   new_size * sizeof(struct st_texture_image_transfer));
254       memset(&stImage->transfer[stImage->num_transfers], 0,
255              (new_size - stImage->num_transfers) *
256              sizeof(struct st_texture_image_transfer));
257       stImage->num_transfers = new_size;
258    }
259 
260    assert(!stImage->transfer[index].transfer);
261    stImage->transfer[index].transfer = transfer;
262 }
263 
264 /* See st_texture.h for more information. */
265 GLuint
st_texture_image_resource_level(struct gl_texture_image * stImage)266 st_texture_image_resource_level(struct gl_texture_image *stImage)
267 {
268    /* An image for a non-finalized texture object only has a single level. */
269    if (stImage->pt != stImage->TexObject->pt)
270       return 0;
271 
272    /* An immutable texture object may have views with an LOD offset. */
273    if (stImage->TexObject->Immutable)
274       return stImage->Level + stImage->TexObject->Attrib.MinLevel;
275 
276    return stImage->Level;
277 }
278 
279 /**
280  * Map a texture image and return the address for a particular 2D face/slice/
281  * layer.  The stImage indicates the cube face and mipmap level.  The slice
282  * of the 3D texture is passed in 'zoffset'.
283  * \param usage  one of the PIPE_MAP_x values
284  * \param x, y, w, h  the region of interest of the 2D image.
285  * \return address of mapping or NULL if any error
286  */
287 GLubyte *
st_texture_image_map(struct st_context * st,struct gl_texture_image * stImage,enum pipe_map_flags usage,GLuint x,GLuint y,GLuint z,GLuint w,GLuint h,GLuint d,struct pipe_transfer ** transfer)288 st_texture_image_map(struct st_context *st, struct gl_texture_image *stImage,
289                      enum pipe_map_flags usage,
290                      GLuint x, GLuint y, GLuint z,
291                      GLuint w, GLuint h, GLuint d,
292                      struct pipe_transfer **transfer)
293 {
294    struct gl_texture_object *stObj = stImage->TexObject;
295    GLuint level;
296    void *map;
297 
298    DBG("%s \n", __func__);
299 
300    if (!stImage->pt)
301       return NULL;
302 
303    if (stObj->pt != stImage->pt)
304       level = 0;
305    else
306       level = stImage->Level;
307 
308    if (stObj->Immutable) {
309       level += stObj->Attrib.MinLevel;
310       z += stObj->Attrib.MinLayer;
311       if (stObj->pt->array_size > 1)
312          d = MIN2(d, stObj->Attrib.NumLayers);
313    }
314 
315    z += stImage->Face;
316 
317    map = pipe_texture_map_3d(st->pipe, stImage->pt, level, usage,
318                               x, y, z, w, h, d, transfer);
319 
320    if (map)
321       st_texture_image_insert_transfer(stImage, z, *transfer);
322 
323    return map;
324 }
325 
326 
327 void
st_texture_image_unmap(struct st_context * st,struct gl_texture_image * stImage,unsigned slice)328 st_texture_image_unmap(struct st_context *st,
329                        struct gl_texture_image *stImage, unsigned slice)
330 {
331    struct pipe_context *pipe = st->pipe;
332    struct gl_texture_object *stObj = stImage->TexObject;
333    struct pipe_transfer **transfer;
334 
335    if (stObj->Immutable)
336       slice += stObj->Attrib.MinLayer;
337    transfer = &stImage->transfer[slice + stImage->Face].transfer;
338 
339    DBG("%s\n", __func__);
340 
341    pipe_texture_unmap(pipe, *transfer);
342    *transfer = NULL;
343 }
344 
345 
346 /**
347  * For debug only: get/print center pixel in the src resource.
348  */
349 static void
print_center_pixel(struct pipe_context * pipe,struct pipe_resource * src)350 print_center_pixel(struct pipe_context *pipe, struct pipe_resource *src)
351 {
352    struct pipe_transfer *xfer;
353    struct pipe_box region;
354    uint8_t *map;
355 
356    region.x = src->width0 / 2;
357    region.y = src->height0 / 2;
358    region.z = 0;
359    region.width = 1;
360    region.height = 1;
361    region.depth = 1;
362 
363    map = pipe->texture_map(pipe, src, 0, PIPE_MAP_READ, &region, &xfer);
364 
365    printf("center pixel: %d %d %d %d\n", map[0], map[1], map[2], map[3]);
366 
367    pipe->texture_unmap(pipe, xfer);
368 }
369 
370 
371 /**
372  * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
373  * This is used to copy mipmap images from one texture buffer to another.
374  * This typically happens when our initial guess at the total texture size
375  * is incorrect (see the guess_and_alloc_texture() function).
376  */
377 void
st_texture_image_copy(struct pipe_context * pipe,struct pipe_resource * dst,GLuint dstLevel,struct pipe_resource * src,GLuint srcLevel,GLuint face)378 st_texture_image_copy(struct pipe_context *pipe,
379                       struct pipe_resource *dst, GLuint dstLevel,
380                       struct pipe_resource *src, GLuint srcLevel,
381                       GLuint face)
382 {
383    GLuint width = u_minify(dst->width0, dstLevel);
384    GLuint height = u_minify(dst->height0, dstLevel);
385    GLuint depth = u_minify(dst->depth0, dstLevel);
386    struct pipe_box src_box;
387    GLuint i;
388 
389    if (u_minify(src->width0, srcLevel) != width ||
390        u_minify(src->height0, srcLevel) != height ||
391        u_minify(src->depth0, srcLevel) != depth) {
392       /* The source image size doesn't match the destination image size.
393        * This can happen in some degenerate situations such as rendering to a
394        * cube map face which was set up with mismatched texture sizes.
395        */
396       return;
397    }
398 
399    src_box.x = 0;
400    src_box.y = 0;
401    src_box.width = width;
402    src_box.height = height;
403    src_box.depth = 1;
404 
405    if (src->target == PIPE_TEXTURE_1D_ARRAY ||
406        src->target == PIPE_TEXTURE_2D_ARRAY ||
407        src->target == PIPE_TEXTURE_CUBE_ARRAY) {
408       face = 0;
409       depth = src->array_size;
410    }
411 
412    /* Loop over 3D image slices */
413    /* could (and probably should) use "true" 3d box here -
414       but drivers can't quite handle it yet */
415    for (i = face; i < face + depth; i++) {
416       src_box.z = i;
417 
418       if (0)  {
419          print_center_pixel(pipe, src);
420       }
421 
422       pipe->resource_copy_region(pipe,
423                                  dst,
424                                  dstLevel,
425                                  0, 0, i,/* destX, Y, Z */
426                                  src,
427                                  srcLevel,
428                                  &src_box);
429    }
430 }
431 
432 
433 struct pipe_resource *
st_create_color_map_texture(struct gl_context * ctx)434 st_create_color_map_texture(struct gl_context *ctx)
435 {
436    struct st_context *st = st_context(ctx);
437    struct pipe_resource *pt;
438    enum pipe_format format;
439    const uint texSize = 256; /* simple, and usually perfect */
440 
441    /* find an RGBA texture format */
442    format = st_choose_format(st, GL_RGBA, GL_NONE, GL_NONE,
443                              PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW,
444                              false, false);
445 
446    /* create texture for color map/table */
447    pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
448                           texSize, texSize, 1, 1, 0, PIPE_BIND_SAMPLER_VIEW, false,
449                           PIPE_COMPRESSION_FIXED_RATE_NONE);
450    return pt;
451 }
452 
453 
454 /**
455  * Destroy bound texture handles for the given stage.
456  */
457 static void
st_destroy_bound_texture_handles_per_stage(struct st_context * st,enum pipe_shader_type shader)458 st_destroy_bound_texture_handles_per_stage(struct st_context *st,
459                                            enum pipe_shader_type shader)
460 {
461    struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader];
462    struct pipe_context *pipe = st->pipe;
463    unsigned i;
464 
465    if (likely(!bound_handles->num_handles))
466       return;
467 
468    for (i = 0; i < bound_handles->num_handles; i++) {
469       uint64_t handle = bound_handles->handles[i];
470 
471       pipe->make_texture_handle_resident(pipe, handle, false);
472       pipe->delete_texture_handle(pipe, handle);
473    }
474    free(bound_handles->handles);
475    bound_handles->handles = NULL;
476    bound_handles->num_handles = 0;
477 }
478 
479 
480 /**
481  * Destroy all bound texture handles in the context.
482  */
483 void
st_destroy_bound_texture_handles(struct st_context * st)484 st_destroy_bound_texture_handles(struct st_context *st)
485 {
486    unsigned i;
487 
488    for (i = 0; i < PIPE_SHADER_TYPES; i++) {
489       st_destroy_bound_texture_handles_per_stage(st, i);
490    }
491 }
492 
493 
494 /**
495  * Destroy bound image handles for the given stage.
496  */
497 static void
st_destroy_bound_image_handles_per_stage(struct st_context * st,enum pipe_shader_type shader)498 st_destroy_bound_image_handles_per_stage(struct st_context *st,
499                                          enum pipe_shader_type shader)
500 {
501    struct st_bound_handles *bound_handles = &st->bound_image_handles[shader];
502    struct pipe_context *pipe = st->pipe;
503    unsigned i;
504 
505    if (likely(!bound_handles->num_handles))
506       return;
507 
508    for (i = 0; i < bound_handles->num_handles; i++) {
509       uint64_t handle = bound_handles->handles[i];
510 
511       pipe->make_image_handle_resident(pipe, handle, GL_READ_WRITE, false);
512       pipe->delete_image_handle(pipe, handle);
513    }
514    free(bound_handles->handles);
515    bound_handles->handles = NULL;
516    bound_handles->num_handles = 0;
517 }
518 
519 
520 /**
521  * Destroy all bound image handles in the context.
522  */
523 void
st_destroy_bound_image_handles(struct st_context * st)524 st_destroy_bound_image_handles(struct st_context *st)
525 {
526    unsigned i;
527 
528    for (i = 0; i < PIPE_SHADER_TYPES; i++) {
529       st_destroy_bound_image_handles_per_stage(st, i);
530    }
531 }
532 
533 
534 /**
535  * Create a texture handle from a texture unit.
536  */
537 static GLuint64
st_create_texture_handle_from_unit(struct st_context * st,struct gl_program * prog,GLuint texUnit)538 st_create_texture_handle_from_unit(struct st_context *st,
539                                    struct gl_program *prog, GLuint texUnit)
540 {
541    struct pipe_context *pipe = st->pipe;
542    struct pipe_sampler_view *view;
543    struct pipe_sampler_state sampler = {0};
544    const bool glsl130 =
545       (prog->shader_program ? prog->shader_program->GLSL_Version : 0) >= 130;
546 
547    /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
548    view = st_update_single_texture(st, texUnit, glsl130, true, false);
549    if (!view)
550       return 0;
551 
552    if (view->target != PIPE_BUFFER)
553       st_convert_sampler_from_unit(st, &sampler, texUnit, glsl130);
554 
555    assert(st->ctx->Texture.Unit[texUnit]._Current);
556 
557    return pipe->create_texture_handle(pipe, view, &sampler);
558 }
559 
560 
561 /**
562  * Create an image handle from an image unit.
563  */
564 static GLuint64
st_create_image_handle_from_unit(struct st_context * st,struct gl_program * prog,GLuint imgUnit)565 st_create_image_handle_from_unit(struct st_context *st,
566                                  struct gl_program *prog, GLuint imgUnit)
567 {
568    struct pipe_context *pipe = st->pipe;
569    struct pipe_image_view img;
570 
571    st_convert_image_from_unit(st, &img, imgUnit, 0);
572 
573    return pipe->create_image_handle(pipe, &img);
574 }
575 
576 
577 /**
578  * Make all bindless samplers bound to texture units resident in the context.
579  */
580 void
st_make_bound_samplers_resident(struct st_context * st,struct gl_program * prog)581 st_make_bound_samplers_resident(struct st_context *st,
582                                 struct gl_program *prog)
583 {
584    enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage);
585    struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader];
586    struct pipe_context *pipe = st->pipe;
587    GLuint64 handle;
588    int i;
589 
590    /* Remove previous bound texture handles for this stage. */
591    st_destroy_bound_texture_handles_per_stage(st, shader);
592 
593    if (likely(!prog->sh.HasBoundBindlessSampler))
594       return;
595 
596    for (i = 0; i < prog->sh.NumBindlessSamplers; i++) {
597       struct gl_bindless_sampler *sampler = &prog->sh.BindlessSamplers[i];
598 
599       if (!sampler->bound)
600          continue;
601 
602       /* Request a new texture handle from the driver and make it resident. */
603       handle = st_create_texture_handle_from_unit(st, prog, sampler->unit);
604       if (!handle)
605          continue;
606 
607       pipe->make_texture_handle_resident(st->pipe, handle, true);
608 
609       /* Overwrite the texture unit value by the resident handle before
610        * uploading the constant buffer.
611        */
612       *(uint64_t *)sampler->data = handle;
613 
614       /* Store the handle in the context. */
615       bound_handles->handles = (uint64_t *)
616          realloc(bound_handles->handles,
617                  (bound_handles->num_handles + 1) * sizeof(uint64_t));
618       bound_handles->handles[bound_handles->num_handles] = handle;
619       bound_handles->num_handles++;
620    }
621 }
622 
623 
624 /**
625  * Make all bindless images bound to image units resident in the context.
626  */
627 void
st_make_bound_images_resident(struct st_context * st,struct gl_program * prog)628 st_make_bound_images_resident(struct st_context *st,
629                               struct gl_program *prog)
630 {
631    enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage);
632    struct st_bound_handles *bound_handles = &st->bound_image_handles[shader];
633    struct pipe_context *pipe = st->pipe;
634    GLuint64 handle;
635    int i;
636 
637    /* Remove previous bound image handles for this stage. */
638    st_destroy_bound_image_handles_per_stage(st, shader);
639 
640    if (likely(!prog->sh.HasBoundBindlessImage))
641       return;
642 
643    for (i = 0; i < prog->sh.NumBindlessImages; i++) {
644       struct gl_bindless_image *image = &prog->sh.BindlessImages[i];
645 
646       if (!image->bound)
647          continue;
648 
649       /* Request a new image handle from the driver and make it resident. */
650       handle = st_create_image_handle_from_unit(st, prog, image->unit);
651       if (!handle)
652          continue;
653 
654       pipe->make_image_handle_resident(st->pipe, handle, GL_READ_WRITE, true);
655 
656       /* Overwrite the image unit value by the resident handle before uploading
657        * the constant buffer.
658        */
659       *(uint64_t *)image->data = handle;
660 
661       /* Store the handle in the context. */
662       bound_handles->handles = (uint64_t *)
663          realloc(bound_handles->handles,
664                  (bound_handles->num_handles + 1) * sizeof(uint64_t));
665       bound_handles->handles[bound_handles->num_handles] = handle;
666       bound_handles->num_handles++;
667    }
668 }
669