radv: don't pass null to _mesa_sha1_update
[mesa.git] / src / amd / vulkan / radv_pipeline_cache.c
index fb83f073405132c1bfa981005304aac3543ab3c2..fe38f3010094b96ae67f4d2842bda29f4c088d9a 100644 (file)
@@ -28,6 +28,7 @@
 #include "radv_debug.h"
 #include "radv_private.h"
 #include "radv_shader.h"
+#include "vulkan/util/vk_util.h"
 
 #include "ac_nir_to_llvm.h"
 
@@ -41,12 +42,31 @@ struct cache_entry {
        char code[0];
 };
 
+static void
+radv_pipeline_cache_lock(struct radv_pipeline_cache *cache)
+{
+       if (cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT_EXT)
+               return;
+
+       pthread_mutex_lock(&cache->mutex);
+}
+
+static void
+radv_pipeline_cache_unlock(struct radv_pipeline_cache *cache)
+{
+       if (cache->flags & VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT_EXT)
+               return;
+
+       pthread_mutex_unlock(&cache->mutex);
+}
+
 void
 radv_pipeline_cache_init(struct radv_pipeline_cache *cache,
                         struct radv_device *device)
 {
        cache->device = device;
        pthread_mutex_init(&cache->mutex, NULL);
+       cache->flags = 0;
 
        cache->modified = false;
        cache->kernel_count = 0;
@@ -113,7 +133,7 @@ radv_hash_shaders(unsigned char *hash,
 
                        _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
                        _mesa_sha1_update(&ctx, stages[i]->pName, strlen(stages[i]->pName));
-                       if (spec_info) {
+                       if (spec_info && spec_info->mapEntryCount) {
                                _mesa_sha1_update(&ctx, spec_info->pMapEntries,
                                                  spec_info->mapEntryCount * sizeof spec_info->pMapEntries[0]);
                                _mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
@@ -156,11 +176,11 @@ radv_pipeline_cache_search(struct radv_pipeline_cache *cache,
 {
        struct cache_entry *entry;
 
-       pthread_mutex_lock(&cache->mutex);
+       radv_pipeline_cache_lock(cache);
 
        entry = radv_pipeline_cache_search_unlocked(cache, sha1);
 
-       pthread_mutex_unlock(&cache->mutex);
+       radv_pipeline_cache_unlock(cache);
 
        return entry;
 }
@@ -243,67 +263,6 @@ radv_is_cache_disabled(struct radv_device *device)
        return (device->instance->debug_flags & RADV_DEBUG_NO_CACHE);
 }
 
-/*
- * Secure compiles cannot open files so we get the parent process to load the
- * cache entry for us.
- */
-static struct cache_entry *
-radv_sc_read_from_disk_cache(struct radv_device *device, uint8_t *disk_sha1)
-{
-       struct cache_entry *entry;
-       unsigned process = device->sc_state->secure_compile_thread_counter;
-       enum radv_secure_compile_type sc_type = RADV_SC_TYPE_READ_DISK_CACHE;
-
-       write(device->sc_state->secure_compile_processes[process].fd_secure_output,
-             &sc_type, sizeof(enum radv_secure_compile_type));
-       write(device->sc_state->secure_compile_processes[process].fd_secure_output,
-             disk_sha1, sizeof(uint8_t) * 20);
-
-       uint8_t found_cache_entry;
-       if (!radv_sc_read(device->sc_state->secure_compile_processes[process].fd_secure_input,
-                         &found_cache_entry, sizeof(uint8_t), true))
-               return NULL;
-
-       if (found_cache_entry) {
-               size_t entry_size;
-               if (!radv_sc_read(device->sc_state->secure_compile_processes[process].fd_secure_input,
-                                 &entry_size, sizeof(size_t), true))
-                       return NULL;
-
-               entry = malloc(entry_size);
-               if (!radv_sc_read(device->sc_state->secure_compile_processes[process].fd_secure_input,
-                                 entry, entry_size, true))
-                       return NULL;
-
-               return entry;
-       }
-
-       return NULL;
-}
-
-/*
- * Secure compiles cannot open files so we get the parent process to write to
- * the disk cache for us.
- */
-static void
-radv_sc_write_to_disk_cache(struct radv_device *device, uint8_t *disk_sha1,
-                           struct cache_entry *entry)
-{
-       unsigned process = device->sc_state->secure_compile_thread_counter;
-       enum radv_secure_compile_type sc_type = RADV_SC_TYPE_WRITE_DISK_CACHE;
-
-       write(device->sc_state->secure_compile_processes[process].fd_secure_output,
-             &sc_type, sizeof(enum radv_secure_compile_type));
-       write(device->sc_state->secure_compile_processes[process].fd_secure_output,
-             disk_sha1, sizeof(uint8_t) * 20);
-
-       uint32_t size = entry_size(entry);
-       write(device->sc_state->secure_compile_processes[process].fd_secure_output,
-             &size, sizeof(uint32_t));
-       write(device->sc_state->secure_compile_processes[process].fd_secure_output,
-             entry, size);
-}
-
 bool
 radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
                                                struct radv_pipeline_cache *cache,
@@ -318,7 +277,7 @@ radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
                *found_in_application_cache = false;
        }
 
-       pthread_mutex_lock(&cache->mutex);
+       radv_pipeline_cache_lock(cache);
 
        entry = radv_pipeline_cache_search_unlocked(cache, sha1);
 
@@ -329,7 +288,7 @@ radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
                 * present in the cache.
                 */
                if (radv_is_cache_disabled(device) || !device->physical_device->disk_cache) {
-                       pthread_mutex_unlock(&cache->mutex);
+                       radv_pipeline_cache_unlock(cache);
                        return false;
                }
 
@@ -337,16 +296,11 @@ radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
                disk_cache_compute_key(device->physical_device->disk_cache,
                                       sha1, 20, disk_sha1);
 
-               if (radv_device_use_secure_compile(device->instance)) {
-                       entry = radv_sc_read_from_disk_cache(device, disk_sha1);
-               } else {
-                       entry = (struct cache_entry *)
-                               disk_cache_get(device->physical_device->disk_cache,
-                                              disk_sha1, NULL);
-               }
-
+               entry = (struct cache_entry *)
+                       disk_cache_get(device->physical_device->disk_cache,
+                                      disk_sha1, NULL);
                if (!entry) {
-                       pthread_mutex_unlock(&cache->mutex);
+                       radv_pipeline_cache_unlock(cache);
                        return false;
                } else {
                        size_t size = entry_size(entry);
@@ -354,7 +308,7 @@ radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
                                                                 VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
                        if (!new_entry) {
                                free(entry);
-                               pthread_mutex_unlock(&cache->mutex);
+                               radv_pipeline_cache_unlock(cache);
                                return false;
                        }
 
@@ -394,7 +348,7 @@ radv_create_shader_variants_from_pipeline_cache(struct radv_device *device,
                                p_atomic_inc(&entry->variants[i]->ref_count);
        }
 
-       pthread_mutex_unlock(&cache->mutex);
+       radv_pipeline_cache_unlock(cache);
        return true;
 }
 
@@ -408,7 +362,7 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device,
        if (!cache)
                cache = device->mem_cache;
 
-       pthread_mutex_lock(&cache->mutex);
+       radv_pipeline_cache_lock(cache);
        struct cache_entry *entry = radv_pipeline_cache_search_unlocked(cache, sha1);
        if (entry) {
                for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
@@ -421,7 +375,7 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device,
                        if (variants[i])
                                p_atomic_inc(&variants[i]->ref_count);
                }
-               pthread_mutex_unlock(&cache->mutex);
+               radv_pipeline_cache_unlock(cache);
                return;
        }
 
@@ -429,7 +383,7 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device,
         * present in the cache.
         */
        if (radv_is_cache_disabled(device)) {
-               pthread_mutex_unlock(&cache->mutex);
+               radv_pipeline_cache_unlock(cache);
                return;
        }
 
@@ -442,7 +396,7 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device,
        entry = vk_alloc(&cache->alloc, size, 8,
                           VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
        if (!entry) {
-               pthread_mutex_unlock(&cache->mutex);
+               radv_pipeline_cache_unlock(cache);
                return;
        }
 
@@ -470,22 +424,14 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device,
                disk_cache_compute_key(device->physical_device->disk_cache, sha1, 20,
                               disk_sha1);
 
-               /* Write the cache item out to the parent of this forked
-                * process.
-                */
-               if (radv_device_use_secure_compile(device->instance)) {
-                       radv_sc_write_to_disk_cache(device, disk_sha1, entry);
-               } else {
-                       disk_cache_put(device->physical_device->disk_cache,
-                                      disk_sha1, entry, entry_size(entry),
-                                      NULL);
-               }
+               disk_cache_put(device->physical_device->disk_cache, disk_sha1,
+                              entry, entry_size(entry), NULL);
        }
 
        if (device->instance->debug_flags & RADV_DEBUG_NO_MEMORY_CACHE &&
            cache == device->mem_cache) {
                vk_free2(&cache->alloc, NULL, entry);
-               pthread_mutex_unlock(&cache->mutex);
+               radv_pipeline_cache_unlock(cache);
                return;
        }
 
@@ -503,24 +449,16 @@ radv_pipeline_cache_insert_shaders(struct radv_device *device,
        radv_pipeline_cache_add_entry(cache, entry);
 
        cache->modified = true;
-       pthread_mutex_unlock(&cache->mutex);
+       radv_pipeline_cache_unlock(cache);
        return;
 }
 
-struct cache_header {
-       uint32_t header_size;
-       uint32_t header_version;
-       uint32_t vendor_id;
-       uint32_t device_id;
-       uint8_t  uuid[VK_UUID_SIZE];
-};
-
 bool
 radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
                         const void *data, size_t size)
 {
        struct radv_device *device = cache->device;
-       struct cache_header header;
+       struct vk_pipeline_cache_header header;
 
        if (size < sizeof(header))
                return false;
@@ -578,12 +516,16 @@ VkResult radv_CreatePipelineCache(
        if (cache == NULL)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       vk_object_base_init(&device->vk, &cache->base,
+                           VK_OBJECT_TYPE_PIPELINE_CACHE);
+
        if (pAllocator)
                cache->alloc = *pAllocator;
        else
                cache->alloc = device->vk.alloc;
 
        radv_pipeline_cache_init(cache, device);
+       cache->flags = pCreateInfo->flags;
 
        if (pCreateInfo->initialDataSize > 0) {
                radv_pipeline_cache_load(cache,
@@ -608,6 +550,7 @@ void radv_DestroyPipelineCache(
                return;
        radv_pipeline_cache_finish(cache);
 
+       vk_object_base_finish(&cache->base);
        vk_free2(&device->vk.alloc, pAllocator, cache);
 }
 
@@ -619,19 +562,19 @@ VkResult radv_GetPipelineCacheData(
 {
        RADV_FROM_HANDLE(radv_device, device, _device);
        RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
-       struct cache_header *header;
+       struct vk_pipeline_cache_header *header;
        VkResult result = VK_SUCCESS;
 
-       pthread_mutex_lock(&cache->mutex);
+       radv_pipeline_cache_lock(cache);
 
        const size_t size = sizeof(*header) + cache->total_size;
        if (pData == NULL) {
-               pthread_mutex_unlock(&cache->mutex);
+               radv_pipeline_cache_unlock(cache);
                *pDataSize = size;
                return VK_SUCCESS;
        }
        if (*pDataSize < sizeof(*header)) {
-               pthread_mutex_unlock(&cache->mutex);
+               radv_pipeline_cache_unlock(cache);
                *pDataSize = 0;
                return VK_INCOMPLETE;
        }
@@ -662,7 +605,7 @@ VkResult radv_GetPipelineCacheData(
        }
        *pDataSize = p - pData;
 
-       pthread_mutex_unlock(&cache->mutex);
+       radv_pipeline_cache_unlock(cache);
        return result;
 }