| 1 |
/* |
| 2 |
* Copyright © 2024 Collabora Ltd. |
| 3 |
* Copyright 2022 Alyssa Rosenzweig |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "genxml/gen_macros.h" |
| 8 |
#include "util/macros.h" |
| 9 |
#include "bifrost/bifrost_compile.h" |
| 10 |
#include "libpan_shaders.h" |
| 11 |
#include "panvk_device.h" |
| 12 |
#include "panvk_precomp_cache.h" |
| 13 |
#include "panvk_shader.h" |
| 14 |
#include "vk_alloc.h" |
| 15 |
#include "vk_shader.h" |
| 16 |
|
| 17 |
struct panvk_precomp_cache * |
| 18 |
panvk_per_arch(precomp_cache_init)(struct panvk_device *dev) |
| 19 |
{ |
| 20 |
struct panvk_precomp_cache *res = |
| 21 |
vk_zalloc(&dev->vk.alloc, sizeof(struct panvk_precomp_cache), 8, |
| 22 |
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 23 |
|
| 24 |
if (res == NULL) |
| 25 |
return NULL; |
| 26 |
|
| 27 |
simple_mtx_init(&res->lock, mtx_plain); |
| 28 |
res->dev = dev; |
| 29 |
res->programs = GENX(libpan_shaders_default); |
| 30 |
|
| 31 |
return res; |
| 32 |
} |
| 33 |
|
| 34 |
void |
| 35 |
panvk_per_arch(precomp_cache_cleanup)(struct panvk_precomp_cache *cache) |
| 36 |
{ |
| 37 |
for (unsigned i = 0; i < ARRAY_SIZE(cache->precomp); i++) { |
| 38 |
if (cache->precomp[i]) |
| 39 |
vk_shader_destroy(&cache->dev->vk, &cache->precomp[i]->vk, |
| 40 |
&cache->dev->vk.alloc); |
| 41 |
} |
| 42 |
|
| 43 |
simple_mtx_destroy(&cache->lock); |
| 44 |
vk_free(&cache->dev->vk.alloc, cache); |
| 45 |
} |
| 46 |
|
| 47 |
static struct panvk_shader * |
| 48 |
panvk_get_precompiled_locked(struct panvk_precomp_cache *cache, |
| 49 |
unsigned program) |
| 50 |
{ |
| 51 |
simple_mtx_assert_locked(&cache->lock); |
| 52 |
|
| 53 |
/* It is possible that, while waiting for the lock, another thread uploaded |
| 54 |
* the shader. Check for that so we don't double-upload. |
| 55 |
*/ |
| 56 |
if (cache->precomp[program]) |
| 57 |
return cache->precomp[program]; |
| 58 |
|
| 59 |
const uint32_t *bin = cache->programs[program]; |
| 60 |
const struct bifrost_precompiled_kernel_info *info = (void *)bin; |
| 61 |
const void *binary = (const uint8_t *)bin + sizeof(*info); |
| 62 |
struct pan_compute_dim local_dim = { |
| 63 |
.x = info->local_size_x, |
| 64 |
.y = info->local_size_y, |
| 65 |
.z = info->local_size_z, |
| 66 |
}; |
| 67 |
|
| 68 |
struct panvk_shader *shader; |
| 69 |
VkResult result = panvk_per_arch(create_shader_from_binary)( |
| 70 |
cache->dev, &info->info, local_dim, binary, info->binary_size, &shader); |
| 71 |
|
| 72 |
if (result != VK_SUCCESS) |
| 73 |
return NULL; |
| 74 |
|
| 75 |
/* We must only write to the cache once we are done compiling, since other |
| 76 |
* threads may be reading the cache concurrently. Do this last. |
| 77 |
*/ |
| 78 |
p_atomic_set(&cache->precomp[program], shader); |
| 79 |
|
| 80 |
return shader; |
| 81 |
} |
| 82 |
|
| 83 |
const struct panvk_shader_variant * |
| 84 |
panvk_per_arch(precomp_cache_get)(struct panvk_precomp_cache *cache, |
| 85 |
unsigned program) |
| 86 |
{ |
| 87 |
/* Shaders are immutable once written, so if we atomically read a non-NULL |
| 88 |
* shader, then we have a valid cached shader and are done. |
| 89 |
*/ |
| 90 |
struct panvk_shader *ret = p_atomic_read(cache->precomp + program); |
| 91 |
|
| 92 |
if (ret != NULL) |
| 93 |
return panvk_shader_only_variant(ret); |
| 94 |
|
| 95 |
/* Otherwise, take the lock and upload. */ |
| 96 |
simple_mtx_lock(&cache->lock); |
| 97 |
ret = panvk_get_precompiled_locked(cache, program); |
| 98 |
simple_mtx_unlock(&cache->lock); |
| 99 |
|
| 100 |
return panvk_shader_only_variant(ret); |
| 101 |
} |