radv: move RADV_TRACE_FILE functions to radv_debug.c
[mesa.git] / src / amd / vulkan / radv_pipeline_cache.c
index 85a2b6dc918787cfb3f7d676003d9ef9fe172858..ef1f513f369cdd69cca263a43023f156a9a1ec45 100644 (file)
@@ -23,6 +23,8 @@
 
 #include "util/mesa-sha1.h"
 #include "util/debug.h"
+#include "util/u_atomic.h"
+#include "radv_debug.h"
 #include "radv_private.h"
 
 #include "ac_nir_to_llvm.h"
@@ -57,7 +59,7 @@ radv_pipeline_cache_init(struct radv_pipeline_cache *cache,
        /* We don't consider allocation failure fatal, we just start with a 0-sized
         * cache. */
        if (cache->hash_table == NULL ||
-           !env_var_as_boolean("RADV_ENABLE_PIPELINE_CACHE", true))
+           (device->debug_flags & RADV_DEBUG_NO_CACHE))
                cache->table_size = 0;
        else
                memset(cache->hash_table, 0, byte_size);
@@ -71,7 +73,7 @@ radv_pipeline_cache_finish(struct radv_pipeline_cache *cache)
                        if (cache->hash_table[i]->variant)
                                radv_shader_variant_destroy(cache->device,
                                                            cache->hash_table[i]->variant);
-                       radv_free(&cache->alloc, cache->hash_table[i]);
+                       vk_free(&cache->alloc, cache->hash_table[i]);
                }
        pthread_mutex_destroy(&cache->mutex);
        free(cache->hash_table);
@@ -88,23 +90,25 @@ radv_hash_shader(unsigned char *hash, struct radv_shader_module *module,
                 const char *entrypoint,
                 const VkSpecializationInfo *spec_info,
                 const struct radv_pipeline_layout *layout,
-                const union ac_shader_variant_key *key)
+                const struct ac_shader_variant_key *key,
+                uint32_t is_geom_copy_shader)
 {
-       struct mesa_sha1 *ctx;
+       struct mesa_sha1 ctx;
 
-       ctx = _mesa_sha1_init();
+       _mesa_sha1_init(&ctx);
        if (key)
-               _mesa_sha1_update(ctx, key, sizeof(*key));
-       _mesa_sha1_update(ctx, module->sha1, sizeof(module->sha1));
-       _mesa_sha1_update(ctx, entrypoint, strlen(entrypoint));
+               _mesa_sha1_update(&ctx, key, sizeof(*key));
+       _mesa_sha1_update(&ctx, module->sha1, sizeof(module->sha1));
+       _mesa_sha1_update(&ctx, entrypoint, strlen(entrypoint));
        if (layout)
-               _mesa_sha1_update(ctx, layout->sha1, sizeof(layout->sha1));
+               _mesa_sha1_update(&ctx, layout->sha1, sizeof(layout->sha1));
        if (spec_info) {
-               _mesa_sha1_update(ctx, spec_info->pMapEntries,
+               _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);
+               _mesa_sha1_update(&ctx, spec_info->pData, spec_info->dataSize);
        }
-       _mesa_sha1_final(ctx, hash);
+       _mesa_sha1_update(&ctx, &is_geom_copy_shader, 4);
+       _mesa_sha1_final(&ctx, hash);
 }
 
 
@@ -115,6 +119,9 @@ radv_pipeline_cache_search_unlocked(struct radv_pipeline_cache *cache,
        const uint32_t mask = cache->table_size - 1;
        const uint32_t start = (*(uint32_t *) sha1);
 
+       if (cache->table_size == 0)
+               return NULL;
+
        for (uint32_t i = 0; i < cache->table_size; i++) {
                const uint32_t index = (start + i) & mask;
                struct cache_entry *entry = cache->hash_table[index];
@@ -150,7 +157,10 @@ radv_create_shader_variant_from_pipeline_cache(struct radv_device *device,
                                               struct radv_pipeline_cache *cache,
                                               const unsigned char *sha1)
 {
-       struct cache_entry *entry = radv_pipeline_cache_search(cache, sha1);
+       struct cache_entry *entry = NULL;
+
+       if (cache)
+               entry = radv_pipeline_cache_search(cache, sha1);
 
        if (!entry)
                return NULL;
@@ -162,23 +172,21 @@ radv_create_shader_variant_from_pipeline_cache(struct radv_device *device,
                if (!variant)
                        return NULL;
 
+               variant->code_size = entry->code_size;
                variant->config = entry->config;
                variant->info = entry->variant_info;
                variant->rsrc1 = entry->rsrc1;
                variant->rsrc2 = entry->rsrc2;
+               variant->code_size = entry->code_size;
                variant->ref_count = 1;
 
-               variant->bo = device->ws->buffer_create(device->ws, entry->code_size, 256,
-                                               RADEON_DOMAIN_GTT, RADEON_FLAG_CPU_ACCESS);
-
-               void *ptr = device->ws->buffer_map(variant->bo);
+               void *ptr = radv_alloc_shader_memory(device, variant);
                memcpy(ptr, entry->code, entry->code_size);
-               device->ws->buffer_unmap(variant->bo);
 
                entry->variant = variant;
        }
 
-       __sync_fetch_and_add(&entry->variant->ref_count, 1);
+       p_atomic_inc(&entry->variant->ref_count);
        return entry->variant;
 }
 
@@ -258,6 +266,9 @@ radv_pipeline_cache_insert_shader(struct radv_pipeline_cache *cache,
                                  struct radv_shader_variant *variant,
                                  const void *code, unsigned code_size)
 {
+       if (!cache)
+               return variant;
+
        pthread_mutex_lock(&cache->mutex);
        struct cache_entry *entry = radv_pipeline_cache_search_unlocked(cache, sha1);
        if (entry) {
@@ -267,12 +278,12 @@ radv_pipeline_cache_insert_shader(struct radv_pipeline_cache *cache,
                } else {
                        entry->variant = variant;
                }
-               __sync_fetch_and_add(&variant->ref_count, 1);
+               p_atomic_inc(&variant->ref_count);
                pthread_mutex_unlock(&cache->mutex);
                return variant;
        }
 
-       entry = radv_alloc(&cache->alloc, sizeof(*entry) + code_size, 8,
+       entry = vk_alloc(&cache->alloc, sizeof(*entry) + code_size, 8,
                           VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
        if (!entry) {
                pthread_mutex_unlock(&cache->mutex);
@@ -287,7 +298,7 @@ radv_pipeline_cache_insert_shader(struct radv_pipeline_cache *cache,
        entry->rsrc2 = variant->rsrc2;
        entry->code_size = code_size;
        entry->variant = variant;
-       __sync_fetch_and_add(&variant->ref_count, 1);
+       p_atomic_inc(&variant->ref_count);
 
        radv_pipeline_cache_add_entry(cache, entry);
 
@@ -303,13 +314,13 @@ struct cache_header {
        uint32_t device_id;
        uint8_t  uuid[VK_UUID_SIZE];
 };
+
 void
 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;
-       uint8_t uuid[VK_UUID_SIZE];
 
        if (size < sizeof(header))
                return;
@@ -320,10 +331,9 @@ radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
                return;
        if (header.vendor_id != 0x1002)
                return;
-       if (header.device_id != device->instance->physicalDevice.rad_info.pci_id)
+       if (header.device_id != device->physical_device->rad_info.pci_id)
                return;
-       radv_device_get_cache_uuid(uuid);
-       if (memcmp(header.uuid, uuid, VK_UUID_SIZE) != 0)
+       if (memcmp(header.uuid, device->physical_device->cache_uuid, VK_UUID_SIZE) != 0)
                return;
 
        char *end = (void *) data + size;
@@ -335,7 +345,7 @@ radv_pipeline_cache_load(struct radv_pipeline_cache *cache,
                if(end - p < sizeof(*entry) + entry->code_size)
                        break;
 
-               dest_entry = radv_alloc(&cache->alloc, sizeof(*entry) + entry->code_size,
+               dest_entry = vk_alloc(&cache->alloc, sizeof(*entry) + entry->code_size,
                                        8, VK_SYSTEM_ALLOCATION_SCOPE_CACHE);
                if (dest_entry) {
                        memcpy(dest_entry, entry, sizeof(*entry) + entry->code_size);
@@ -358,7 +368,7 @@ VkResult radv_CreatePipelineCache(
        assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO);
        assert(pCreateInfo->flags == 0);
 
-       cache = radv_alloc2(&device->alloc, pAllocator,
+       cache = vk_alloc2(&device->alloc, pAllocator,
                            sizeof(*cache), 8,
                            VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (cache == NULL)
@@ -394,7 +404,7 @@ void radv_DestroyPipelineCache(
                return;
        radv_pipeline_cache_finish(cache);
 
-       radv_free2(&device->alloc, pAllocator, cache);
+       vk_free2(&device->alloc, pAllocator, cache);
 }
 
 VkResult radv_GetPipelineCacheData(
@@ -421,8 +431,8 @@ VkResult radv_GetPipelineCacheData(
        header->header_size = sizeof(*header);
        header->header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE;
        header->vendor_id = 0x1002;
-       header->device_id = device->instance->physicalDevice.rad_info.pci_id;
-       radv_device_get_cache_uuid(header->uuid);
+       header->device_id = device->physical_device->rad_info.pci_id;
+       memcpy(header->uuid, device->physical_device->cache_uuid, VK_UUID_SIZE);
        p += header->header_size;
 
        struct cache_entry *entry;