|
14 | 14 |
|
15 | 15 | namespace impeller {
|
16 | 16 |
|
17 |
| -static NSArray<id<MTLLibrary>>* ShaderLibrariesFromFiles( |
18 |
| - id<MTLDevice> device, |
19 |
| - const std::vector<std::string>& libraries_paths) { |
20 |
| - NSMutableArray<id<MTLLibrary>>* found_libraries = [NSMutableArray array]; |
21 |
| - for (const auto& library_path : libraries_paths) { |
22 |
| - if (!fml::IsFile(library_path)) { |
23 |
| - VALIDATION_LOG << "Shader library does not exist at path '" |
24 |
| - << library_path << "'"; |
25 |
| - continue; |
26 |
| - } |
27 |
| - NSError* shader_library_error = nil; |
28 |
| - auto library = [device newLibraryWithFile:@(library_path.c_str()) |
29 |
| - error:&shader_library_error]; |
30 |
| - if (!library) { |
31 |
| - FML_LOG(ERROR) << "Could not create shader library: " |
32 |
| - << shader_library_error.localizedDescription.UTF8String; |
33 |
| - continue; |
34 |
| - } |
35 |
| - [found_libraries addObject:library]; |
36 |
| - } |
37 |
| - return found_libraries; |
38 |
| -} |
39 |
| - |
40 |
| -ContextMTL::ContextMTL(const std::vector<std::string>& libraries_paths) |
41 |
| - : device_(::MTLCreateSystemDefaultDevice()) { |
42 |
| - // Setup device. |
| 17 | +ContextMTL::ContextMTL(id<MTLDevice> device, |
| 18 | + NSArray<id<MTLLibrary>>* shader_libraries) |
| 19 | + : device_(device) { |
| 20 | + // Validate device. |
43 | 21 | if (!device_) {
|
| 22 | + VALIDATION_LOG << "Could not setup valid Metal device."; |
44 | 23 | return;
|
45 | 24 | }
|
46 | 25 |
|
| 26 | + // Setup the shader library. |
| 27 | + { |
| 28 | + if (shader_libraries == nil) { |
| 29 | + VALIDATION_LOG << "Shader libraries were null."; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // std::make_shared disallowed because of private friend ctor. |
| 34 | + auto library = std::shared_ptr<ShaderLibraryMTL>( |
| 35 | + new ShaderLibraryMTL(shader_libraries)); |
| 36 | + if (!library->IsValid()) { |
| 37 | + VALIDATION_LOG << "Could not create valid Metal shader library."; |
| 38 | + return; |
| 39 | + } |
| 40 | + shader_library_ = std::move(library); |
| 41 | + } |
| 42 | + |
47 | 43 | // Setup command queues.
|
48 | 44 | render_queue_ = device_.newCommandQueue;
|
49 | 45 | transfer_queue_ = device_.newCommandQueue;
|
|
55 | 51 | render_queue_.label = @"Impeller Render Queue";
|
56 | 52 | transfer_queue_.label = @"Impeller Transfer Queue";
|
57 | 53 |
|
58 |
| - // Setup the shader library. |
59 |
| - { |
60 |
| - // std::make_shared disallowed because of private friend ctor. |
61 |
| - auto library = std::shared_ptr<ShaderLibraryMTL>(new ShaderLibraryMTL( |
62 |
| - ShaderLibrariesFromFiles(device_, libraries_paths))); |
63 |
| - if (!library->IsValid()) { |
64 |
| - VALIDATION_LOG << "Could not create valid Metal shader library."; |
65 |
| - return; |
66 |
| - } |
67 |
| - shader_library_ = std::move(library); |
68 |
| - } |
69 |
| - |
70 | 54 | // Setup the pipeline library.
|
71 | 55 | { //
|
72 | 56 | pipeline_library_ =
|
|
96 | 80 | is_valid_ = true;
|
97 | 81 | }
|
98 | 82 |
|
| 83 | +static NSArray<id<MTLLibrary>>* MTLShaderLibraryFromFilePaths( |
| 84 | + id<MTLDevice> device, |
| 85 | + const std::vector<std::string>& libraries_paths) { |
| 86 | + NSMutableArray<id<MTLLibrary>>* found_libraries = [NSMutableArray array]; |
| 87 | + for (const auto& library_path : libraries_paths) { |
| 88 | + if (!fml::IsFile(library_path)) { |
| 89 | + VALIDATION_LOG << "Shader library does not exist at path '" |
| 90 | + << library_path << "'"; |
| 91 | + return nil; |
| 92 | + } |
| 93 | + NSError* shader_library_error = nil; |
| 94 | + auto library = [device newLibraryWithFile:@(library_path.c_str()) |
| 95 | + error:&shader_library_error]; |
| 96 | + if (!library) { |
| 97 | + FML_LOG(ERROR) << "Could not create shader library: " |
| 98 | + << shader_library_error.localizedDescription.UTF8String; |
| 99 | + return nil; |
| 100 | + } |
| 101 | + [found_libraries addObject:library]; |
| 102 | + } |
| 103 | + return found_libraries; |
| 104 | +} |
| 105 | + |
| 106 | +static NSArray<id<MTLLibrary>>* MTLShaderLibraryFromFileData( |
| 107 | + id<MTLDevice> device, |
| 108 | + const std::vector<std::shared_ptr<fml::Mapping>>& libraries_data) { |
| 109 | + NSMutableArray<id<MTLLibrary>>* found_libraries = [NSMutableArray array]; |
| 110 | + for (const auto& library_data : libraries_data) { |
| 111 | + if (library_data == nullptr) { |
| 112 | + FML_LOG(ERROR) << "Shader library data was null."; |
| 113 | + return nil; |
| 114 | + } |
| 115 | + |
| 116 | + __block auto data = library_data; |
| 117 | + |
| 118 | + auto dispatch_data = |
| 119 | + ::dispatch_data_create(library_data->GetMapping(), // buffer |
| 120 | + library_data->GetSize(), // size |
| 121 | + dispatch_get_main_queue(), // queue |
| 122 | + ^() { |
| 123 | + // We just need a reference. |
| 124 | + data.reset(); |
| 125 | + } // destructor |
| 126 | + ); |
| 127 | + if (!dispatch_data) { |
| 128 | + FML_LOG(ERROR) << "Could not wrap shader data in dispatch data."; |
| 129 | + return nil; |
| 130 | + } |
| 131 | + |
| 132 | + NSError* shader_library_error = nil; |
| 133 | + auto library = [device newLibraryWithData:dispatch_data |
| 134 | + error:&shader_library_error]; |
| 135 | + if (!library) { |
| 136 | + FML_LOG(ERROR) << "Could not create shader library: " |
| 137 | + << shader_library_error.localizedDescription.UTF8String; |
| 138 | + return nil; |
| 139 | + } |
| 140 | + [found_libraries addObject:library]; |
| 141 | + } |
| 142 | + return found_libraries; |
| 143 | +} |
| 144 | + |
| 145 | +static id<MTLDevice> CreateMetalDevice() { |
| 146 | + return ::MTLCreateSystemDefaultDevice(); |
| 147 | +} |
| 148 | + |
| 149 | +std::shared_ptr<Context> ContextMTL::Create( |
| 150 | + const std::vector<std::string>& shader_library_paths) { |
| 151 | + auto device = CreateMetalDevice(); |
| 152 | + auto context = std::shared_ptr<ContextMTL>(new ContextMTL( |
| 153 | + device, MTLShaderLibraryFromFilePaths(device, shader_library_paths))); |
| 154 | + if (!context->IsValid()) { |
| 155 | + FML_LOG(ERROR) << "Could not create Metal context."; |
| 156 | + return nullptr; |
| 157 | + } |
| 158 | + return context; |
| 159 | +} |
| 160 | + |
| 161 | +std::shared_ptr<Context> ContextMTL::Create( |
| 162 | + const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data) { |
| 163 | + auto device = CreateMetalDevice(); |
| 164 | + auto context = std::shared_ptr<ContextMTL>(new ContextMTL( |
| 165 | + device, MTLShaderLibraryFromFileData(device, shader_libraries_data))); |
| 166 | + if (!context->IsValid()) { |
| 167 | + FML_LOG(ERROR) << "Could not create Metal context."; |
| 168 | + return nullptr; |
| 169 | + } |
| 170 | + return context; |
| 171 | +} |
| 172 | + |
99 | 173 | ContextMTL::~ContextMTL() = default;
|
100 | 174 |
|
101 | 175 | bool ContextMTL::IsValid() const {
|
|
0 commit comments