radv: add a workaround for Monster Hunter World and LLVM 7&8
[mesa.git] / src / amd / vulkan / radv_meta.c
index a59f38fb21d3018a9a96c47ea50276c9e6fb69dd..ec4fc4a6d4bf03fa248edda20496a421ecdbb95d 100644 (file)
@@ -234,15 +234,12 @@ radv_builtin_cache_path(char *path)
        const char *suffix2 = "/.cache/radv_builtin_shaders";
        struct passwd pwd, *result;
        char path2[PATH_MAX + 1]; /* PATH_MAX is not a real max,but suffices here. */
+       int ret;
 
        if (xdg_cache_home) {
-
-               if (strlen(xdg_cache_home) + strlen(suffix) > PATH_MAX)
-                       return false;
-
-               strcpy(path, xdg_cache_home);
-               strcat(path, suffix);
-               return true;
+               ret = snprintf(path, PATH_MAX + 1, "%s%s%zd",
+                              xdg_cache_home, suffix, sizeof(void *) * 8);
+               return ret > 0 && ret < PATH_MAX + 1;
        }
 
        getpwuid_r(getuid(), &pwd, path2, PATH_MAX - strlen(suffix2), &result);
@@ -253,23 +250,25 @@ radv_builtin_cache_path(char *path)
        strcat(path, "/.cache");
        mkdir(path, 0755);
 
-       strcat(path, suffix);
-       return true;
+       ret = snprintf(path, PATH_MAX + 1, "%s%s%zd",
+                      pwd.pw_dir, suffix2, sizeof(void *) * 8);
+       return ret > 0 && ret < PATH_MAX + 1;
 }
 
-static void
+static bool
 radv_load_meta_pipeline(struct radv_device *device)
 {
        char path[PATH_MAX + 1];
        struct stat st;
        void *data = NULL;
+       bool ret = false;
 
        if (!radv_builtin_cache_path(path))
-               return;
+               return false;
 
        int fd = open(path, O_RDONLY);
        if (fd < 0)
-               return;
+               return false;
        if (fstat(fd, &st))
                goto fail;
        data = malloc(st.st_size);
@@ -278,10 +277,11 @@ radv_load_meta_pipeline(struct radv_device *device)
        if(read(fd, data, st.st_size) == -1)
                goto fail;
 
-       radv_pipeline_cache_load(&device->meta_state.cache, data, st.st_size);
+       ret = radv_pipeline_cache_load(&device->meta_state.cache, data, st.st_size);
 fail:
        free(data);
        close(fd);
+       return ret;
 }
 
 static void
@@ -330,6 +330,8 @@ radv_device_init_meta(struct radv_device *device)
 {
        VkResult result;
 
+       memset(&device->meta_state, 0, sizeof(device->meta_state));
+
        device->meta_state.alloc = (VkAllocationCallbacks) {
                .pUserData = device,
                .pfnAllocation = meta_alloc,
@@ -339,21 +341,24 @@ radv_device_init_meta(struct radv_device *device)
 
        device->meta_state.cache.alloc = device->meta_state.alloc;
        radv_pipeline_cache_init(&device->meta_state.cache, device);
-       radv_load_meta_pipeline(device);
+       bool loaded_cache = radv_load_meta_pipeline(device);
+       bool on_demand = !loaded_cache;
 
-       result = radv_device_init_meta_clear_state(device);
+       mtx_init(&device->meta_state.mtx, mtx_plain);
+
+       result = radv_device_init_meta_clear_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_clear;
 
-       result = radv_device_init_meta_resolve_state(device);
+       result = radv_device_init_meta_resolve_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_resolve;
 
-       result = radv_device_init_meta_blit_state(device);
+       result = radv_device_init_meta_blit_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_blit;
 
-       result = radv_device_init_meta_blit2d_state(device);
+       result = radv_device_init_meta_blit2d_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_blit2d;
 
@@ -361,7 +366,7 @@ radv_device_init_meta(struct radv_device *device)
        if (result != VK_SUCCESS)
                goto fail_bufimage;
 
-       result = radv_device_init_meta_depth_decomp_state(device);
+       result = radv_device_init_meta_depth_decomp_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_depth_decomp;
 
@@ -369,23 +374,30 @@ radv_device_init_meta(struct radv_device *device)
        if (result != VK_SUCCESS)
                goto fail_buffer;
 
-       result = radv_device_init_meta_query_state(device);
+       result = radv_device_init_meta_query_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_query;
 
-       result = radv_device_init_meta_fast_clear_flush_state(device);
+       result = radv_device_init_meta_fast_clear_flush_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_fast_clear;
 
-       result = radv_device_init_meta_resolve_compute_state(device);
+       result = radv_device_init_meta_resolve_compute_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_resolve_compute;
 
-       result = radv_device_init_meta_resolve_fragment_state(device);
+       result = radv_device_init_meta_resolve_fragment_state(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail_resolve_fragment;
+
+       result = radv_device_init_meta_fmask_expand_state(device);
+       if (result != VK_SUCCESS)
+               goto fail_fmask_expand;
+
        return VK_SUCCESS;
 
+fail_fmask_expand:
+       radv_device_finish_meta_resolve_fragment_state(device);
 fail_resolve_fragment:
        radv_device_finish_meta_resolve_compute_state(device);
 fail_resolve_compute:
@@ -407,6 +419,7 @@ fail_blit:
 fail_resolve:
        radv_device_finish_meta_clear_state(device);
 fail_clear:
+       mtx_destroy(&device->meta_state.mtx);
        radv_pipeline_cache_finish(&device->meta_state.cache);
        return result;
 }
@@ -425,9 +438,11 @@ radv_device_finish_meta(struct radv_device *device)
        radv_device_finish_meta_fast_clear_flush_state(device);
        radv_device_finish_meta_resolve_compute_state(device);
        radv_device_finish_meta_resolve_fragment_state(device);
+       radv_device_finish_meta_fmask_expand_state(device);
 
        radv_store_meta_pipeline(device);
        radv_pipeline_cache_finish(&device->meta_state.cache);
+       mtx_destroy(&device->meta_state.mtx);
 }
 
 nir_ssa_def *radv_meta_gen_rect_vertices_comp2(nir_builder *vs_b, nir_ssa_def *comp2)