Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Friends | List of all members
impeller::ShaderLibraryMTL Class Referencefinal

#include <shader_library_mtl.h>

Inheritance diagram for impeller::ShaderLibraryMTL:
impeller::ShaderLibrary

Public Member Functions

 ShaderLibraryMTL ()
 
 ~ShaderLibraryMTL () override
 
bool IsValid () const override
 
- Public Member Functions inherited from impeller::ShaderLibrary
virtual ~ShaderLibrary ()
 

Private Member Functions

std::shared_ptr< const ShaderFunctionGetFunction (std::string_view name, ShaderStage stage) override
 
void RegisterFunction (std::string name, ShaderStage stage, std::shared_ptr< fml::Mapping > code, RegistrationCallback callback) override
 
void UnregisterFunction (std::string name, ShaderStage stage) override
 

Friends

class ContextMTL
 

Additional Inherited Members

- Public Types inherited from impeller::ShaderLibrary
using RegistrationCallback = std::function< void(bool)>
 
- Protected Member Functions inherited from impeller::ShaderLibrary
 ShaderLibrary ()
 

Detailed Description

Definition at line 23 of file shader_library_mtl.h.

Constructor & Destructor Documentation

◆ ShaderLibraryMTL()

impeller::ShaderLibraryMTL::ShaderLibraryMTL ( )

◆ ~ShaderLibraryMTL()

impeller::ShaderLibraryMTL::~ShaderLibraryMTL ( )
overridedefault

Member Function Documentation

◆ GetFunction()

std::shared_ptr< const ShaderFunction > impeller::ShaderLibraryMTL::GetFunction ( std::string_view  name,
ShaderStage  stage 
)
overrideprivatevirtual

Implements impeller::ShaderLibrary.

Definition at line 41 of file shader_library_mtl.mm.

43 {
44 if (!IsValid()) {
45 return nullptr;
46 }
47
48 if (name.empty()) {
49 VALIDATION_LOG << "Library function name was empty.";
50 return nullptr;
51 }
52
53 ShaderKey key(name, stage);
54
55 id<MTLFunction> function = nil;
56 id<MTLLibrary> library = nil;
57
58 {
59 ReaderLock lock(libraries_mutex_);
60
61 if (auto found = functions_.find(key); found != functions_.end()) {
62 return found->second;
63 }
64
65 for (size_t i = 0, count = [libraries_ count]; i < count; i++) {
66 library = libraries_[i];
67 function = [library newFunctionWithName:@(name.data())];
68 if (function) {
69 break;
70 }
71 }
72
73 if (function == nil) {
74 return nullptr;
75 }
76
77 if (function.functionType != ToMTLFunctionType(stage)) {
78 VALIDATION_LOG << "Library function named " << name
79 << " was for an unexpected shader stage.";
80 return nullptr;
81 }
82
83 auto func = std::shared_ptr<ShaderFunctionMTL>(new ShaderFunctionMTL(
84 library_id_, function, library, {name.data(), name.size()}, stage));
85 functions_[key] = func;
86
87 return func;
88 }
89}
int count
bool IsValid() const override
Dart_NativeFunction function
Definition fuchsia.cc:51
const char * name
Definition fuchsia.cc:50
static MTLFunctionType ToMTLFunctionType(ShaderStage stage)
#define VALIDATION_LOG
Definition validation.h:73

◆ IsValid()

bool impeller::ShaderLibraryMTL::IsValid ( ) const
overridevirtual

Implements impeller::ShaderLibrary.

Definition at line 24 of file shader_library_mtl.mm.

24 {
25 return is_valid_;
26}

◆ RegisterFunction()

void impeller::ShaderLibraryMTL::RegisterFunction ( std::string  name,
ShaderStage  stage,
std::shared_ptr< fml::Mapping code,
RegistrationCallback  callback 
)
overrideprivatevirtual

Reimplemented from impeller::ShaderLibrary.

Definition at line 100 of file shader_library_mtl.mm.

103 {
104 if (!callback) {
105 callback = [](auto) {};
106 }
107 auto failure_callback = std::make_shared<fml::ScopedCleanupClosure>(
108 [callback]() { callback(false); });
109 if (!IsValid()) {
110 return;
111 }
112 if (code == nullptr || code->GetMapping() == nullptr) {
113 return;
114 }
115 auto device = GetDevice();
116 if (device == nil) {
117 return;
118 }
119
120 auto source = [[NSString alloc] initWithBytes:code->GetMapping()
121 length:code->GetSize()
122 encoding:NSUTF8StringEncoding];
123
124 auto weak_this = weak_from_this();
125 [device newLibraryWithSource:source
126 options:NULL
127 completionHandler:^(id<MTLLibrary> library, NSError* error) {
128 auto strong_this = weak_this.lock();
129 if (!strong_this) {
130 VALIDATION_LOG << "Shader library was collected before "
131 "dynamic shader stage could be registered.";
132 return;
133 }
134 if (!library) {
135 VALIDATION_LOG << "Could not register dynamic stage library: "
136 << error.localizedDescription.UTF8String;
137 return;
138 }
139 reinterpret_cast<ShaderLibraryMTL*>(strong_this.get())
140 ->RegisterLibrary(library);
141 failure_callback->Release();
142 callback(true);
143 }];
144}
const char * options
VkDevice device
Definition main.cc:53
SkBitmap source
Definition examples.cpp:28
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
const uint8_t uint32_t uint32_t GError ** error
size_t length

◆ UnregisterFunction()

void impeller::ShaderLibraryMTL::UnregisterFunction ( std::string  name,
ShaderStage  stage 
)
overrideprivatevirtual

Implements impeller::ShaderLibrary.

Definition at line 147 of file shader_library_mtl.mm.

147 {
148 ReaderLock lock(libraries_mutex_);
149
150 // Find the shader library containing this function name and remove it.
151
152 bool found_library = false;
153 for (size_t i = [libraries_ count] - 1; i >= 0; i--) {
154 id<MTLFunction> function =
155 [libraries_[i] newFunctionWithName:@(name.data())];
156 if (function) {
157 [libraries_ removeObjectAtIndex:i];
158 found_library = true;
159 break;
160 }
161 }
162 if (!found_library) {
163 VALIDATION_LOG << "Library containing function " << name
164 << " was not found, so it couldn't be unregistered.";
165 }
166
167 // Remove the shader from the function cache.
168
169 ShaderKey key(name, stage);
170
171 auto found = functions_.find(key);
172 if (found == functions_.end()) {
173 VALIDATION_LOG << "Library function named " << name
174 << " was not found, so it couldn't be unregistered.";
175 return;
176 }
177
178 functions_.erase(found);
179}

Friends And Related Symbol Documentation

◆ ContextMTL

friend class ContextMTL
friend

Definition at line 34 of file shader_library_mtl.h.


The documentation for this class was generated from the following files: