radv: implement radv_shader_create_cached()
authorDaniel Schürmann <daniel@schuermann.dev>
Mon, 20 Mar 2023 13:34:35 +0000 (14:34 +0100)
committerMarge Bot <emma+marge@anholt.net>
Tue, 11 Apr 2023 11:38:15 +0000 (11:38 +0000)
This function takes a radv_shader_binary and writes it to the
disk cache before creating and returning a radv_shader cache entry.
The key of the cache entry is the full SHA1 hash of the binary.
This way, we will be able to deduplicate identical shaders.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22030>

src/amd/vulkan/radv_pipeline_cache.c
src/amd/vulkan/radv_shader.h

index a220075..97f7130 100644 (file)
@@ -279,6 +279,25 @@ radv_shader_serialize(struct vk_pipeline_cache_object *object, struct blob *blob
    return true;
 }
 
+struct radv_shader *
+radv_shader_create_cached(struct radv_device *device, struct vk_pipeline_cache *cache,
+                          const struct radv_shader_binary *binary)
+{
+   if (radv_is_cache_disabled(device))
+      return radv_shader_create(device, binary);
+
+   uint8_t hash[SHA1_DIGEST_LENGTH];
+   _mesa_sha1_compute(binary, binary->total_size, hash);
+
+   /* TODO: Skip disk-cache for meta-shaders because they are stored in a different cache file */
+
+   struct vk_pipeline_cache_object *shader_obj;
+   shader_obj = vk_pipeline_cache_create_and_insert_object(cache, hash, SHA1_DIGEST_LENGTH, binary,
+                                                           binary->total_size, &radv_shader_ops);
+
+   return shader_obj ? container_of(shader_obj, struct radv_shader, base) : NULL;
+}
+
 const struct vk_pipeline_cache_object_ops radv_shader_ops = {
    .serialize = radv_shader_serialize,
    .deserialize = radv_shader_deserialize,
index 1b5b2e9..b40e9c6 100644 (file)
@@ -572,6 +572,11 @@ struct radv_shader_args;
 
 struct radv_shader *radv_shader_create(struct radv_device *device,
                                        const struct radv_shader_binary *binary);
+
+struct radv_shader *radv_shader_create_cached(struct radv_device *device,
+                                              struct vk_pipeline_cache *cache,
+                                              const struct radv_shader_binary *binary);
+
 struct radv_shader *radv_shader_nir_to_asm(
    struct radv_device *device, struct radv_pipeline_stage *stage, struct nir_shader *const *shaders,
    int shader_count, const struct radv_pipeline_key *key, bool keep_shader_info, bool keep_statistic_info,