| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "kk_device.h" |
| 8 |
|
| 9 |
#include "kk_shader.h" |
| 10 |
|
| 11 |
#include "kosmickrisp/clc/kk_precompiled_shader.h" |
| 12 |
|
| 13 |
#include "libkk_shaders.h" |
| 14 |
|
| 15 |
#include "kosmickrisp/bridge/mtl_bridge.h" |
| 16 |
|
| 17 |
#include "nir/nir.h" |
| 18 |
#include "nir/nir_builder.h" |
| 19 |
|
| 20 |
static VkResult |
| 21 |
build_precompiled_shaders(struct kk_device *dev) |
| 22 |
{ |
| 23 |
VkResult result = VK_SUCCESS; |
| 24 |
uint32_t i = 0u; |
| 25 |
for (; i < LIBKK_NUM_PROGRAMS; ++i) { |
| 26 |
const uint32_t *bin = libkk_AppleSilicon[i]; |
| 27 |
const struct kk_precompiled_info *info = (void *)bin; |
| 28 |
const char *msl = (const char *)bin + sizeof(*info); |
| 29 |
|
| 30 |
mtl_library *library = mtl_new_library(dev->mtl_handle, msl); |
| 31 |
if (library == NULL) |
| 32 |
goto fail; |
| 33 |
|
| 34 |
struct kk_precompiled_shader *shader = &dev->precompiled_cache.shaders[i]; |
| 35 |
|
| 36 |
|
| 37 |
mtl_function *function = |
| 38 |
mtl_new_function_with_name(library, "main_entrypoint"); |
| 39 |
uint32_t local_size_threads = info->workgroup_size[0] * |
| 40 |
info->workgroup_size[1] * |
| 41 |
info->workgroup_size[2]; |
| 42 |
shader->pipeline = mtl_new_compute_pipeline_state( |
| 43 |
dev->mtl_handle, function, local_size_threads); |
| 44 |
mtl_release(function); |
| 45 |
mtl_release(library); |
| 46 |
|
| 47 |
if (!shader->pipeline) |
| 48 |
goto fail; |
| 49 |
|
| 50 |
memcpy(&shader->info, info, sizeof(*info)); |
| 51 |
} |
| 52 |
|
| 53 |
return result; |
| 54 |
|
| 55 |
fail: |
| 56 |
for (uint32_t j = 0u; j < i; ++j) |
| 57 |
mtl_release(dev->precompiled_cache.shaders[j].pipeline); |
| 58 |
return vk_error(dev, result); |
| 59 |
} |
| 60 |
|
| 61 |
VkResult |
| 62 |
kk_device_init_lib(struct kk_device *dev) |
| 63 |
{ |
| 64 |
return build_precompiled_shaders(dev); |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
kk_device_finish_lib(struct kk_device *dev) |
| 69 |
{ |
| 70 |
for (uint32_t i = 0; i < LIBKK_NUM_PROGRAMS; ++i) |
| 71 |
mtl_release(dev->precompiled_cache.shaders[i].pipeline); |
| 72 |
} |