14 : libraries_([libraries mutableCopy]) {
15 if (libraries_ == nil || libraries_.count == 0) {
22ShaderLibraryMTL::~ShaderLibraryMTL() =
default;
24bool ShaderLibraryMTL::IsValid()
const {
30 case ShaderStage::kVertex:
31 return MTLFunctionTypeVertex;
32 case ShaderStage::kFragment:
33 return MTLFunctionTypeFragment;
34 case ShaderStage::kUnknown:
35 case ShaderStage::kCompute:
36 return MTLFunctionTypeKernel;
41std::shared_ptr<const ShaderFunction> ShaderLibraryMTL::GetFunction(
42 std::string_view
name,
56 id<MTLLibrary> library = nil;
59 ReaderLock lock(libraries_mutex_);
61 if (
auto found = functions_.find(
key); found != functions_.end()) {
65 for (
size_t i = 0, count = [libraries_ count];
i < count;
i++) {
66 library = libraries_[
i];
73 if (function == nil) {
79 <<
" was for an unexpected shader stage.";
83 auto func = std::shared_ptr<ShaderFunctionMTL>(
new ShaderFunctionMTL(
84 library_id_, function, library, {
name.data(),
name.size()}, stage));
85 functions_[
key] = func;
91id<MTLDevice> ShaderLibraryMTL::GetDevice()
const {
92 ReaderLock lock(libraries_mutex_);
93 if (libraries_.count > 0u) {
94 return libraries_[0].device;
100void ShaderLibraryMTL::RegisterFunction(std::string
name,
102 std::shared_ptr<fml::Mapping> code,
107 auto failure_callback = std::make_shared<fml::ScopedCleanupClosure>(
112 if (code ==
nullptr || code->GetMapping() ==
nullptr) {
115 auto device = GetDevice();
120 auto source = [[NSString alloc] initWithBytes:code->GetMapping()
122 encoding:NSUTF8StringEncoding];
124 auto weak_this = weak_from_this();
125 [
device newLibraryWithSource:source
127 completionHandler:^(id<MTLLibrary> library, NSError*
error) {
128 auto strong_this = weak_this.lock();
131 "dynamic shader stage could be registered.";
136 <<
error.localizedDescription.UTF8String;
139 reinterpret_cast<ShaderLibraryMTL*
>(strong_this.get())
140 ->RegisterLibraryAndCacheFunction(library,
name, stage);
141 failure_callback->Release();
147void ShaderLibraryMTL::UnregisterFunction(std::string
name, ShaderStage stage) {
148 WriterLock lock(libraries_mutex_);
156 if (
auto found = functions_.find(
key); found != functions_.end()) {
157 id<MTLLibrary> target_library =
158 ShaderFunctionMTL::Cast(*found->second).library_;
159 if (target_library) {
160 [libraries_ removeObject:target_library];
162 functions_.erase(found);
170 bool found_library =
false;
171 for (
size_t i = [libraries_ count] - 1;
i >= 0;
i--) {
173 [libraries_[
i] newFunctionWithName:@(
name.data())];
175 [libraries_ removeObjectAtIndex:
i];
176 found_library =
true;
180 if (!found_library) {
182 <<
" was not found, so it couldn't be unregistered.";
186void ShaderLibraryMTL::RegisterLibraryAndCacheFunction(id<MTLLibrary> library,
187 const std::string&
name,
189 WriterLock lock(libraries_mutex_);
190 [libraries_ addObject:library];
198 for (NSString* function_name in [library functionNames]) {
199 id<MTLFunction> mtl_function = [library newFunctionWithName:function_name];
200 if (mtl_function && mtl_function.functionType == expected) {
203 std::shared_ptr<ShaderFunctionMTL>(
new ShaderFunctionMTL(
204 library_id_, mtl_function, library,
name, stage));
const uint8_t uint32_t uint32_t GError ** error
FlutterDesktopBinaryReply callback
#define FML_UNREACHABLE()
Dart_NativeFunction function
static MTLFunctionType ToMTLFunctionType(ShaderStage stage)