11#include "spdlog/spdlog.h"
17 for (
const auto& [_, pipeline] : _cache) {
31 const auto it = _cache.find(shader->id());
32 if (it != _cache.end()) {
36 auto* pipeline = create(shader);
38 _cache.emplace(shader->id(), pipeline);
43 MTL::ComputePipelineState* MetalComputePipeline::create(
const std::shared_ptr<Shader>& shader)
49 auto* metalDevice =
const_cast<MTL::Device*
>(
_device->
raw());
54 auto* metalShader =
dynamic_cast<MetalShader*
>(shader.get());
56 spdlog::error(
"Unsupported shader implementation for Metal compute pipeline. Expected MetalShader.");
60 NS::Error* error =
nullptr;
61 const auto* bundle = NS::Bundle::mainBundle();
62 auto* library = metalShader->getLibrary(metalDevice, bundle, &error);
64 spdlog::error(
"Failed to get Metal library for compute shader: {}",
65 error ? error->localizedDescription()->utf8String() :
"unknown");
70 const auto& computeEntry = shader->computeEntry().empty() ? std::string(
"computeMain") : shader->computeEntry();
71 auto* computeFunction = library->newFunction(NS::String::string(computeEntry.c_str(), NS::UTF8StringEncoding));
72 if (!computeFunction) {
73 spdlog::error(
"Failed to find compute shader entry point '{}'", computeEntry);
78 auto* pipeline = metalDevice->newComputePipelineState(computeFunction, &error);
80 spdlog::error(
"Failed to create compute pipeline state: {}",
81 error ? error->localizedDescription()->utf8String() :
"unknown");
84 computeFunction->release();