|
4 | 4 |
|
5 | 5 | #include "impeller/renderer/backend/metal/context_mtl.h"
|
6 | 6 |
|
| 7 | +#include <Foundation/Foundation.h> |
| 8 | + |
7 | 9 | #include "flutter/fml/file.h"
|
8 | 10 | #include "flutter/fml/logging.h"
|
9 | 11 | #include "flutter/fml/paths.h"
|
|
12 | 14 |
|
13 | 15 | namespace impeller {
|
14 | 16 |
|
15 |
| -ContextMTL::ContextMTL(std::string shaders_directory, |
16 |
| - std::string main_library_file_name) |
| 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 | + FML_LOG(ERROR) << "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) |
17 | 41 | : device_(::MTLCreateSystemDefaultDevice()) {
|
18 | 42 | // Setup device.
|
19 | 43 | if (!device_) {
|
|
33 | 57 |
|
34 | 58 | // Setup the shader library.
|
35 | 59 | {
|
36 |
| - NSError* shader_library_error = nil; |
37 |
| - auto shader_library_path = |
38 |
| - fml::paths::JoinPaths({shaders_directory, main_library_file_name}); |
39 |
| - |
40 |
| - auto library_exists = fml::IsFile(shader_library_path); |
41 |
| - |
42 |
| - if (!library_exists) { |
43 |
| - FML_LOG(ERROR) << "Shader library does not exist at path '" |
44 |
| - << shader_library_path |
45 |
| - << "'. No piplines can be created in this context."; |
46 |
| - } |
47 |
| - auto library = |
48 |
| - library_exists |
49 |
| - ? [device_ newLibraryWithFile:@(shader_library_path.c_str()) |
50 |
| - error:&shader_library_error] |
51 |
| - : [device_ newDefaultLibrary]; |
52 |
| - if (!library && shader_library_error) { |
53 |
| - FML_LOG(ERROR) << "Could not create shader library: " |
54 |
| - << shader_library_error.localizedDescription.UTF8String; |
| 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 | + FML_DLOG(ERROR) << "Could not create valid Metal shader library."; |
55 | 65 | return;
|
56 | 66 | }
|
57 |
| - |
58 |
| - // std::make_shared disallowed because of private friend ctor. |
59 |
| - shader_library_ = |
60 |
| - std::shared_ptr<ShaderLibraryMTL>(new ShaderLibraryMTL(library)); |
| 67 | + shader_library_ = std::move(library); |
61 | 68 | }
|
62 | 69 |
|
63 | 70 | // Setup the pipeline library.
|
|
0 commit comments