• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat
3  * based on intel anv code:
4  * Copyright © 2015 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  */
25 
26 #include "radv_meta.h"
27 
28 #include "vk_common_entrypoints.h"
29 #include "vk_pipeline_cache.h"
30 #include "vk_util.h"
31 
32 #include <fcntl.h>
33 #include <limits.h>
34 #ifndef _WIN32
35 #include <pwd.h>
36 #endif
37 #include <sys/stat.h>
38 
39 static void
radv_suspend_queries(struct radv_meta_saved_state * state,struct radv_cmd_buffer * cmd_buffer)40 radv_suspend_queries(struct radv_meta_saved_state *state, struct radv_cmd_buffer *cmd_buffer)
41 {
42    const uint32_t num_pipeline_stat_queries = radv_get_num_pipeline_stat_queries(cmd_buffer);
43 
44    if (num_pipeline_stat_queries > 0) {
45       cmd_buffer->state.flush_bits &= ~RADV_CMD_FLAG_START_PIPELINE_STATS;
46       cmd_buffer->state.flush_bits |= RADV_CMD_FLAG_STOP_PIPELINE_STATS;
47    }
48 
49    /* Pipeline statistics queries. */
50    if (cmd_buffer->state.active_pipeline_queries > 0) {
51       state->active_pipeline_gds_queries = cmd_buffer->state.active_pipeline_gds_queries;
52       cmd_buffer->state.active_pipeline_gds_queries = 0;
53       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_SHADER_QUERY;
54    }
55 
56    /* Occlusion queries. */
57    if (cmd_buffer->state.active_occlusion_queries) {
58       state->active_occlusion_queries = cmd_buffer->state.active_occlusion_queries;
59       cmd_buffer->state.active_occlusion_queries = 0;
60       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_OCCLUSION_QUERY;
61    }
62 
63    /* Primitives generated queries (legacy). */
64    if (cmd_buffer->state.active_prims_gen_queries) {
65       cmd_buffer->state.suspend_streamout = true;
66       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_STREAMOUT_ENABLE;
67    }
68 
69    /* Primitives generated queries (NGG). */
70    if (cmd_buffer->state.active_prims_gen_gds_queries) {
71       state->active_prims_gen_gds_queries = cmd_buffer->state.active_prims_gen_gds_queries;
72       cmd_buffer->state.active_prims_gen_gds_queries = 0;
73       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_SHADER_QUERY;
74    }
75 
76    /* Transform feedback queries (NGG). */
77    if (cmd_buffer->state.active_prims_xfb_gds_queries) {
78       state->active_prims_xfb_gds_queries = cmd_buffer->state.active_prims_xfb_gds_queries;
79       cmd_buffer->state.active_prims_xfb_gds_queries = 0;
80       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_SHADER_QUERY;
81    }
82 }
83 
84 static void
radv_resume_queries(const struct radv_meta_saved_state * state,struct radv_cmd_buffer * cmd_buffer)85 radv_resume_queries(const struct radv_meta_saved_state *state, struct radv_cmd_buffer *cmd_buffer)
86 {
87    const uint32_t num_pipeline_stat_queries = radv_get_num_pipeline_stat_queries(cmd_buffer);
88 
89    if (num_pipeline_stat_queries > 0) {
90       cmd_buffer->state.flush_bits &= ~RADV_CMD_FLAG_STOP_PIPELINE_STATS;
91       cmd_buffer->state.flush_bits |= RADV_CMD_FLAG_START_PIPELINE_STATS;
92    }
93 
94    /* Pipeline statistics queries. */
95    if (cmd_buffer->state.active_pipeline_queries > 0) {
96       cmd_buffer->state.active_pipeline_gds_queries = state->active_pipeline_gds_queries;
97       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_SHADER_QUERY;
98    }
99 
100    /* Occlusion queries. */
101    if (state->active_occlusion_queries) {
102       cmd_buffer->state.active_occlusion_queries = state->active_occlusion_queries;
103       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_OCCLUSION_QUERY;
104    }
105 
106    /* Primitives generated queries (legacy). */
107    if (cmd_buffer->state.active_prims_gen_queries) {
108       cmd_buffer->state.suspend_streamout = false;
109       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_STREAMOUT_ENABLE;
110    }
111 
112    /* Primitives generated queries (NGG). */
113    if (state->active_prims_gen_gds_queries) {
114       cmd_buffer->state.active_prims_gen_gds_queries = state->active_prims_gen_gds_queries;
115       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_SHADER_QUERY;
116    }
117 
118    /* Transform feedback queries (NGG). */
119    if (state->active_prims_xfb_gds_queries) {
120       cmd_buffer->state.active_prims_xfb_gds_queries = state->active_prims_xfb_gds_queries;
121       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_SHADER_QUERY;
122    }
123 }
124 
125 void
radv_meta_save(struct radv_meta_saved_state * state,struct radv_cmd_buffer * cmd_buffer,uint32_t flags)126 radv_meta_save(struct radv_meta_saved_state *state, struct radv_cmd_buffer *cmd_buffer, uint32_t flags)
127 {
128    VkPipelineBindPoint bind_point =
129       flags & RADV_META_SAVE_GRAPHICS_PIPELINE ? VK_PIPELINE_BIND_POINT_GRAPHICS : VK_PIPELINE_BIND_POINT_COMPUTE;
130    struct radv_descriptor_state *descriptors_state = radv_get_descriptors_state(cmd_buffer, bind_point);
131 
132    assert(flags & (RADV_META_SAVE_GRAPHICS_PIPELINE | RADV_META_SAVE_COMPUTE_PIPELINE));
133 
134    state->flags = flags;
135    state->active_occlusion_queries = 0;
136    state->active_prims_gen_gds_queries = 0;
137    state->active_prims_xfb_gds_queries = 0;
138 
139    if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE) {
140       assert(!(state->flags & RADV_META_SAVE_COMPUTE_PIPELINE));
141 
142       state->old_graphics_pipeline = cmd_buffer->state.graphics_pipeline;
143 
144       for (unsigned i = 0; i <= MESA_SHADER_MESH; i++) {
145          if (i == MESA_SHADER_COMPUTE)
146             continue;
147 
148          state->old_shader_objs[i] = cmd_buffer->state.shader_objs[i];
149       }
150 
151       /* Save all dynamic states. */
152       state->dynamic = cmd_buffer->state.dynamic;
153    }
154 
155    if (state->flags & RADV_META_SAVE_COMPUTE_PIPELINE) {
156       assert(!(state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE));
157 
158       state->old_compute_pipeline = cmd_buffer->state.compute_pipeline;
159 
160       state->old_shader_objs[MESA_SHADER_COMPUTE] = cmd_buffer->state.shader_objs[MESA_SHADER_COMPUTE];
161    }
162 
163    if (state->flags & RADV_META_SAVE_DESCRIPTORS) {
164       state->old_descriptor_set0 = descriptors_state->sets[0];
165       if (!(descriptors_state->valid & 1))
166          state->flags &= ~RADV_META_SAVE_DESCRIPTORS;
167    }
168 
169    if (state->flags & RADV_META_SAVE_CONSTANTS) {
170       memcpy(state->push_constants, cmd_buffer->push_constants, MAX_PUSH_CONSTANTS_SIZE);
171    }
172 
173    if (state->flags & RADV_META_SAVE_RENDER) {
174       state->render = cmd_buffer->state.render;
175       radv_cmd_buffer_reset_rendering(cmd_buffer);
176    }
177 
178    if (state->flags & RADV_META_SUSPEND_PREDICATING) {
179       state->predicating = cmd_buffer->state.predicating;
180       cmd_buffer->state.predicating = false;
181    }
182 
183    radv_suspend_queries(state, cmd_buffer);
184 }
185 
186 void
radv_meta_restore(const struct radv_meta_saved_state * state,struct radv_cmd_buffer * cmd_buffer)187 radv_meta_restore(const struct radv_meta_saved_state *state, struct radv_cmd_buffer *cmd_buffer)
188 {
189    VkPipelineBindPoint bind_point = state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE ? VK_PIPELINE_BIND_POINT_GRAPHICS
190                                                                                     : VK_PIPELINE_BIND_POINT_COMPUTE;
191 
192    if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE) {
193       if (state->old_graphics_pipeline) {
194          radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_GRAPHICS,
195                               radv_pipeline_to_handle(&state->old_graphics_pipeline->base));
196       } else {
197          cmd_buffer->state.graphics_pipeline = NULL;
198 
199          for (unsigned i = 0; i <= MESA_SHADER_MESH; i++) {
200             if (i == MESA_SHADER_COMPUTE)
201                continue;
202 
203             if (!state->old_shader_objs[i])
204                continue;
205 
206             VkShaderEXT old_shader_obj = radv_shader_object_to_handle(state->old_shader_objs[i]);
207             VkShaderStageFlagBits s = mesa_to_vk_shader_stage(i);
208 
209             radv_CmdBindShadersEXT(radv_cmd_buffer_to_handle(cmd_buffer), 1, &s, &old_shader_obj);
210          }
211       }
212 
213       /* Restore all dynamic states. */
214       cmd_buffer->state.dynamic = state->dynamic;
215       cmd_buffer->state.dirty |= RADV_DYNAMIC_ALL;
216 
217       /* Re-emit the guardband state because meta operations changed dynamic states. */
218       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_GUARDBAND;
219    }
220 
221    if (state->flags & RADV_META_SAVE_COMPUTE_PIPELINE) {
222       if (state->old_compute_pipeline) {
223          radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,
224                               radv_pipeline_to_handle(&state->old_compute_pipeline->base));
225       } else {
226          cmd_buffer->state.compute_pipeline = NULL;
227 
228          if (state->old_shader_objs[MESA_SHADER_COMPUTE]) {
229             VkShaderEXT old_shader_obj = radv_shader_object_to_handle(state->old_shader_objs[MESA_SHADER_COMPUTE]);
230             VkShaderStageFlagBits s = VK_SHADER_STAGE_COMPUTE_BIT;
231 
232             radv_CmdBindShadersEXT(radv_cmd_buffer_to_handle(cmd_buffer), 1, &s, &old_shader_obj);
233          }
234       }
235    }
236 
237    if (state->flags & RADV_META_SAVE_DESCRIPTORS) {
238       radv_set_descriptor_set(cmd_buffer, bind_point, state->old_descriptor_set0, 0);
239    }
240 
241    if (state->flags & RADV_META_SAVE_CONSTANTS) {
242       VkShaderStageFlags stages = VK_SHADER_STAGE_COMPUTE_BIT;
243 
244       if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE)
245          stages |= VK_SHADER_STAGE_ALL_GRAPHICS;
246 
247       vk_common_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer), VK_NULL_HANDLE, stages, 0,
248                                  MAX_PUSH_CONSTANTS_SIZE, state->push_constants);
249    }
250 
251    if (state->flags & RADV_META_SAVE_RENDER) {
252       cmd_buffer->state.render = state->render;
253       cmd_buffer->state.dirty |= RADV_CMD_DIRTY_FRAMEBUFFER;
254    }
255 
256    if (state->flags & RADV_META_SUSPEND_PREDICATING)
257       cmd_buffer->state.predicating = state->predicating;
258 
259    radv_resume_queries(state, cmd_buffer);
260 }
261 
262 VkImageViewType
radv_meta_get_view_type(const struct radv_image * image)263 radv_meta_get_view_type(const struct radv_image *image)
264 {
265    switch (image->vk.image_type) {
266    case VK_IMAGE_TYPE_1D:
267       return VK_IMAGE_VIEW_TYPE_1D;
268    case VK_IMAGE_TYPE_2D:
269       return VK_IMAGE_VIEW_TYPE_2D;
270    case VK_IMAGE_TYPE_3D:
271       return VK_IMAGE_VIEW_TYPE_3D;
272    default:
273       unreachable("bad VkImageViewType");
274    }
275 }
276 
277 /**
278  * When creating a destination VkImageView, this function provides the needed
279  * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
280  */
281 uint32_t
radv_meta_get_iview_layer(const struct radv_image * dst_image,const VkImageSubresourceLayers * dst_subresource,const VkOffset3D * dst_offset)282 radv_meta_get_iview_layer(const struct radv_image *dst_image, const VkImageSubresourceLayers *dst_subresource,
283                           const VkOffset3D *dst_offset)
284 {
285    switch (dst_image->vk.image_type) {
286    case VK_IMAGE_TYPE_1D:
287    case VK_IMAGE_TYPE_2D:
288       return dst_subresource->baseArrayLayer;
289    case VK_IMAGE_TYPE_3D:
290       /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
291        * but meta does it anyway. When doing so, we translate the
292        * destination's z offset into an array offset.
293        */
294       return dst_offset->z;
295    default:
296       assert(!"bad VkImageType");
297       return 0;
298    }
299 }
300 
301 static VKAPI_ATTR void *VKAPI_CALL
meta_alloc(void * _device,size_t size,size_t alignment,VkSystemAllocationScope allocationScope)302 meta_alloc(void *_device, size_t size, size_t alignment, VkSystemAllocationScope allocationScope)
303 {
304    struct radv_device *device = _device;
305    return device->vk.alloc.pfnAllocation(device->vk.alloc.pUserData, size, alignment,
306                                          VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
307 }
308 
309 static VKAPI_ATTR void *VKAPI_CALL
meta_realloc(void * _device,void * original,size_t size,size_t alignment,VkSystemAllocationScope allocationScope)310 meta_realloc(void *_device, void *original, size_t size, size_t alignment, VkSystemAllocationScope allocationScope)
311 {
312    struct radv_device *device = _device;
313    return device->vk.alloc.pfnReallocation(device->vk.alloc.pUserData, original, size, alignment,
314                                            VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
315 }
316 
317 static VKAPI_ATTR void VKAPI_CALL
meta_free(void * _device,void * data)318 meta_free(void *_device, void *data)
319 {
320    struct radv_device *device = _device;
321    device->vk.alloc.pfnFree(device->vk.alloc.pUserData, data);
322 }
323 
324 #ifndef _WIN32
325 static bool
radv_builtin_cache_path(char * path)326 radv_builtin_cache_path(char *path)
327 {
328    char *xdg_cache_home = secure_getenv("XDG_CACHE_HOME");
329    const char *suffix = "/radv_builtin_shaders";
330    const char *suffix2 = "/.cache/radv_builtin_shaders";
331    struct passwd pwd, *result;
332    char path2[PATH_MAX + 1]; /* PATH_MAX is not a real max,but suffices here. */
333    int ret;
334 
335    if (xdg_cache_home) {
336       ret = snprintf(path, PATH_MAX + 1, "%s%s%zd", xdg_cache_home, suffix, sizeof(void *) * 8);
337       return ret > 0 && ret < PATH_MAX + 1;
338    }
339 
340    getpwuid_r(getuid(), &pwd, path2, PATH_MAX - strlen(suffix2), &result);
341    if (!result)
342       return false;
343 
344    strcpy(path, pwd.pw_dir);
345    strcat(path, "/.cache");
346    if (mkdir(path, 0755) && errno != EEXIST)
347       return false;
348 
349    ret = snprintf(path, PATH_MAX + 1, "%s%s%zd", pwd.pw_dir, suffix2, sizeof(void *) * 8);
350    return ret > 0 && ret < PATH_MAX + 1;
351 }
352 #endif
353 
354 static uint32_t
num_cache_entries(VkPipelineCache cache)355 num_cache_entries(VkPipelineCache cache)
356 {
357    struct set *s = vk_pipeline_cache_from_handle(cache)->object_cache;
358    if (!s)
359       return 0;
360    return s->entries;
361 }
362 
363 static bool
radv_load_meta_pipeline(struct radv_device * device)364 radv_load_meta_pipeline(struct radv_device *device)
365 {
366 #ifdef _WIN32
367    return false;
368 #else
369    char path[PATH_MAX + 1];
370    struct stat st;
371    void *data = NULL;
372    bool ret = false;
373    int fd = -1;
374    struct vk_pipeline_cache *cache = NULL;
375 
376    VkPipelineCacheCreateInfo create_info = {
377       .sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO,
378    };
379 
380    struct vk_pipeline_cache_create_info info = {
381       .pCreateInfo = &create_info,
382       .skip_disk_cache = true,
383    };
384 
385    if (!radv_builtin_cache_path(path))
386       goto fail;
387 
388    fd = open(path, O_RDONLY);
389    if (fd < 0)
390       goto fail;
391    if (fstat(fd, &st))
392       goto fail;
393    data = malloc(st.st_size);
394    if (!data)
395       goto fail;
396    if (read(fd, data, st.st_size) == -1)
397       goto fail;
398 
399    create_info.initialDataSize = st.st_size;
400    create_info.pInitialData = data;
401 
402 fail:
403    cache = vk_pipeline_cache_create(&device->vk, &info, NULL);
404 
405    if (cache) {
406       device->meta_state.cache = vk_pipeline_cache_to_handle(cache);
407       device->meta_state.initial_cache_entries = num_cache_entries(device->meta_state.cache);
408       ret = device->meta_state.initial_cache_entries > 0;
409    }
410 
411    free(data);
412    if (fd >= 0)
413       close(fd);
414    return ret;
415 #endif
416 }
417 
418 static void
radv_store_meta_pipeline(struct radv_device * device)419 radv_store_meta_pipeline(struct radv_device *device)
420 {
421 #ifndef _WIN32
422    char path[PATH_MAX + 1], path2[PATH_MAX + 7];
423    size_t size;
424    void *data = NULL;
425 
426    if (device->meta_state.cache == VK_NULL_HANDLE)
427       return;
428 
429    /* Skip serialization if no entries were added. */
430    if (num_cache_entries(device->meta_state.cache) <= device->meta_state.initial_cache_entries)
431       return;
432 
433    if (vk_common_GetPipelineCacheData(radv_device_to_handle(device), device->meta_state.cache, &size, NULL))
434       return;
435 
436    if (!radv_builtin_cache_path(path))
437       return;
438 
439    strcpy(path2, path);
440    strcat(path2, "XXXXXX");
441    int fd = mkstemp(path2); // open(path, O_WRONLY | O_CREAT, 0600);
442    if (fd < 0)
443       return;
444    data = malloc(size);
445    if (!data)
446       goto fail;
447 
448    if (vk_common_GetPipelineCacheData(radv_device_to_handle(device), device->meta_state.cache, &size, data))
449       goto fail;
450    if (write(fd, data, size) == -1)
451       goto fail;
452 
453    rename(path2, path);
454 fail:
455    free(data);
456    close(fd);
457    unlink(path2);
458 #endif
459 }
460 
461 VkResult
radv_device_init_meta(struct radv_device * device)462 radv_device_init_meta(struct radv_device *device)
463 {
464    VkResult result;
465 
466    memset(&device->meta_state, 0, sizeof(device->meta_state));
467 
468    device->meta_state.alloc = (VkAllocationCallbacks){
469       .pUserData = device,
470       .pfnAllocation = meta_alloc,
471       .pfnReallocation = meta_realloc,
472       .pfnFree = meta_free,
473    };
474 
475    bool loaded_cache = radv_load_meta_pipeline(device);
476    bool on_demand = !loaded_cache;
477 
478    mtx_init(&device->meta_state.mtx, mtx_plain);
479 
480    result = radv_device_init_meta_clear_state(device, on_demand);
481    if (result != VK_SUCCESS)
482       goto fail_clear;
483 
484    result = radv_device_init_meta_resolve_state(device, on_demand);
485    if (result != VK_SUCCESS)
486       goto fail_resolve;
487 
488    result = radv_device_init_meta_blit_state(device, on_demand);
489    if (result != VK_SUCCESS)
490       goto fail_blit;
491 
492    result = radv_device_init_meta_blit2d_state(device, on_demand);
493    if (result != VK_SUCCESS)
494       goto fail_blit2d;
495 
496    result = radv_device_init_meta_bufimage_state(device);
497    if (result != VK_SUCCESS)
498       goto fail_bufimage;
499 
500    result = radv_device_init_meta_depth_decomp_state(device, on_demand);
501    if (result != VK_SUCCESS)
502       goto fail_depth_decomp;
503 
504    result = radv_device_init_meta_buffer_state(device);
505    if (result != VK_SUCCESS)
506       goto fail_buffer;
507 
508    result = radv_device_init_meta_query_state(device, on_demand);
509    if (result != VK_SUCCESS)
510       goto fail_query;
511 
512    result = radv_device_init_meta_fast_clear_flush_state(device, on_demand);
513    if (result != VK_SUCCESS)
514       goto fail_fast_clear;
515 
516    result = radv_device_init_meta_resolve_compute_state(device, on_demand);
517    if (result != VK_SUCCESS)
518       goto fail_resolve_compute;
519 
520    result = radv_device_init_meta_resolve_fragment_state(device, on_demand);
521    if (result != VK_SUCCESS)
522       goto fail_resolve_fragment;
523 
524    if (device->physical_device->use_fmask) {
525       result = radv_device_init_meta_fmask_expand_state(device, on_demand);
526       if (result != VK_SUCCESS)
527          goto fail_fmask_expand;
528 
529       result = radv_device_init_meta_fmask_copy_state(device, on_demand);
530       if (result != VK_SUCCESS)
531          goto fail_fmask_copy;
532    }
533 
534    result = radv_device_init_meta_etc_decode_state(device, on_demand);
535    if (result != VK_SUCCESS)
536       goto fail_etc_decode;
537 
538    result = radv_device_init_meta_astc_decode_state(device, on_demand);
539    if (result != VK_SUCCESS)
540       goto fail_astc_decode;
541 
542    if (radv_uses_device_generated_commands(device)) {
543       result = radv_device_init_dgc_prepare_state(device);
544       if (result != VK_SUCCESS)
545          goto fail_dgc;
546    }
547 
548    if (device->vk.enabled_extensions.KHR_acceleration_structure) {
549       if (device->vk.enabled_features.nullDescriptor) {
550          result = radv_device_init_null_accel_struct(device);
551          if (result != VK_SUCCESS)
552             goto fail_accel_struct;
553       }
554 
555       /* FIXME: Acceleration structure builds hang when the build shaders are compiled with LLVM.
556        * Work around it by forcing ACO for now.
557        */
558       bool use_llvm = device->physical_device->use_llvm;
559       if (loaded_cache || use_llvm) {
560          device->physical_device->use_llvm = false;
561          result = radv_device_init_accel_struct_build_state(device);
562          device->physical_device->use_llvm = use_llvm;
563 
564          if (result != VK_SUCCESS)
565             goto fail_accel_struct;
566       }
567    }
568 
569    return VK_SUCCESS;
570 
571 fail_accel_struct:
572    radv_device_finish_accel_struct_build_state(device);
573 fail_dgc:
574    radv_device_finish_dgc_prepare_state(device);
575 fail_astc_decode:
576    radv_device_finish_meta_astc_decode_state(device);
577 fail_etc_decode:
578    radv_device_finish_meta_etc_decode_state(device);
579 fail_fmask_copy:
580    radv_device_finish_meta_fmask_copy_state(device);
581 fail_fmask_expand:
582    radv_device_finish_meta_fmask_expand_state(device);
583 fail_resolve_fragment:
584    radv_device_finish_meta_resolve_fragment_state(device);
585 fail_resolve_compute:
586    radv_device_finish_meta_resolve_compute_state(device);
587 fail_fast_clear:
588    radv_device_finish_meta_fast_clear_flush_state(device);
589 fail_query:
590    radv_device_finish_meta_query_state(device);
591 fail_buffer:
592    radv_device_finish_meta_buffer_state(device);
593 fail_depth_decomp:
594    radv_device_finish_meta_depth_decomp_state(device);
595 fail_bufimage:
596    radv_device_finish_meta_bufimage_state(device);
597 fail_blit2d:
598    radv_device_finish_meta_blit2d_state(device);
599 fail_blit:
600    radv_device_finish_meta_blit_state(device);
601 fail_resolve:
602    radv_device_finish_meta_resolve_state(device);
603 fail_clear:
604    radv_device_finish_meta_clear_state(device);
605 
606    mtx_destroy(&device->meta_state.mtx);
607    vk_common_DestroyPipelineCache(radv_device_to_handle(device), device->meta_state.cache, NULL);
608    return result;
609 }
610 
611 void
radv_device_finish_meta(struct radv_device * device)612 radv_device_finish_meta(struct radv_device *device)
613 {
614    radv_device_finish_dgc_prepare_state(device);
615    radv_device_finish_meta_etc_decode_state(device);
616    radv_device_finish_meta_astc_decode_state(device);
617    radv_device_finish_accel_struct_build_state(device);
618    radv_device_finish_meta_clear_state(device);
619    radv_device_finish_meta_resolve_state(device);
620    radv_device_finish_meta_blit_state(device);
621    radv_device_finish_meta_blit2d_state(device);
622    radv_device_finish_meta_bufimage_state(device);
623    radv_device_finish_meta_depth_decomp_state(device);
624    radv_device_finish_meta_query_state(device);
625    radv_device_finish_meta_buffer_state(device);
626    radv_device_finish_meta_fast_clear_flush_state(device);
627    radv_device_finish_meta_resolve_compute_state(device);
628    radv_device_finish_meta_resolve_fragment_state(device);
629    radv_device_finish_meta_fmask_expand_state(device);
630    radv_device_finish_meta_dcc_retile_state(device);
631    radv_device_finish_meta_copy_vrs_htile_state(device);
632    radv_device_finish_meta_fmask_copy_state(device);
633 
634    radv_store_meta_pipeline(device);
635    vk_common_DestroyPipelineCache(radv_device_to_handle(device), device->meta_state.cache, NULL);
636    mtx_destroy(&device->meta_state.mtx);
637 }
638 
639 nir_builder PRINTFLIKE(3, 4)
radv_meta_init_shader(struct radv_device * dev,gl_shader_stage stage,const char * name,...)640    radv_meta_init_shader(struct radv_device *dev, gl_shader_stage stage, const char *name, ...)
641 {
642    nir_builder b = nir_builder_init_simple_shader(stage, NULL, NULL);
643    if (name) {
644       va_list args;
645       va_start(args, name);
646       b.shader->info.name = ralloc_vasprintf(b.shader, name, args);
647       va_end(args);
648    }
649 
650    b.shader->options = &dev->physical_device->nir_options[stage];
651 
652    return b;
653 }
654 
655 /* vertex shader that generates vertices */
656 nir_shader *
radv_meta_build_nir_vs_generate_vertices(struct radv_device * dev)657 radv_meta_build_nir_vs_generate_vertices(struct radv_device *dev)
658 {
659    const struct glsl_type *vec4 = glsl_vec4_type();
660 
661    nir_variable *v_position;
662 
663    nir_builder b = radv_meta_init_shader(dev, MESA_SHADER_VERTEX, "meta_vs_gen_verts");
664 
665    nir_def *outvec = nir_gen_rect_vertices(&b, NULL, NULL);
666 
667    v_position = nir_variable_create(b.shader, nir_var_shader_out, vec4, "gl_Position");
668    v_position->data.location = VARYING_SLOT_POS;
669 
670    nir_store_var(&b, v_position, outvec, 0xf);
671 
672    return b.shader;
673 }
674 
675 nir_shader *
radv_meta_build_nir_fs_noop(struct radv_device * dev)676 radv_meta_build_nir_fs_noop(struct radv_device *dev)
677 {
678    return radv_meta_init_shader(dev, MESA_SHADER_FRAGMENT, "meta_noop_fs").shader;
679 }
680 
681 void
radv_meta_build_resolve_shader_core(struct radv_device * device,nir_builder * b,bool is_integer,int samples,nir_variable * input_img,nir_variable * color,nir_def * img_coord)682 radv_meta_build_resolve_shader_core(struct radv_device *device, nir_builder *b, bool is_integer, int samples,
683                                     nir_variable *input_img, nir_variable *color, nir_def *img_coord)
684 {
685    nir_deref_instr *input_img_deref = nir_build_deref_var(b, input_img);
686    nir_def *sample0 = nir_txf_ms_deref(b, input_img_deref, img_coord, nir_imm_int(b, 0));
687 
688    if (is_integer || samples <= 1) {
689       nir_store_var(b, color, sample0, 0xf);
690       return;
691    }
692 
693    if (device->physical_device->use_fmask) {
694       nir_def *all_same = nir_samples_identical_deref(b, input_img_deref, img_coord);
695       nir_push_if(b, nir_inot(b, all_same));
696    }
697 
698    nir_def *accum = sample0;
699    for (int i = 1; i < samples; i++) {
700       nir_def *sample = nir_txf_ms_deref(b, input_img_deref, img_coord, nir_imm_int(b, i));
701       accum = nir_fadd(b, accum, sample);
702    }
703 
704    accum = nir_fdiv_imm(b, accum, samples);
705    nir_store_var(b, color, accum, 0xf);
706 
707    if (device->physical_device->use_fmask) {
708       nir_push_else(b, NULL);
709       nir_store_var(b, color, sample0, 0xf);
710       nir_pop_if(b, NULL);
711    }
712 }
713 
714 nir_def *
radv_meta_load_descriptor(nir_builder * b,unsigned desc_set,unsigned binding)715 radv_meta_load_descriptor(nir_builder *b, unsigned desc_set, unsigned binding)
716 {
717    nir_def *rsrc = nir_vulkan_resource_index(b, 3, 32, nir_imm_int(b, 0), .desc_set = desc_set, .binding = binding);
718    return nir_trim_vector(b, rsrc, 2);
719 }
720 
721 nir_def *
get_global_ids(nir_builder * b,unsigned num_components)722 get_global_ids(nir_builder *b, unsigned num_components)
723 {
724    unsigned mask = BITFIELD_MASK(num_components);
725 
726    nir_def *local_ids = nir_channels(b, nir_load_local_invocation_id(b), mask);
727    nir_def *block_ids = nir_channels(b, nir_load_workgroup_id(b), mask);
728    nir_def *block_size =
729       nir_channels(b,
730                    nir_imm_ivec4(b, b->shader->info.workgroup_size[0], b->shader->info.workgroup_size[1],
731                                  b->shader->info.workgroup_size[2], 0),
732                    mask);
733 
734    return nir_iadd(b, nir_imul(b, block_ids, block_size), local_ids);
735 }
736 
737 void
radv_break_on_count(nir_builder * b,nir_variable * var,nir_def * count)738 radv_break_on_count(nir_builder *b, nir_variable *var, nir_def *count)
739 {
740    nir_def *counter = nir_load_var(b, var);
741 
742    nir_push_if(b, nir_uge(b, counter, count));
743    nir_jump(b, nir_jump_break);
744    nir_pop_if(b, NULL);
745 
746    counter = nir_iadd_imm(b, counter, 1);
747    nir_store_var(b, var, counter, 0x1);
748 }
749