QGVAPMetalShaderFunctionLoader.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // QGVAPMetalShaderFunctionLoader.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGVAPMetalShaderFunctionLoader.h"
  16. #import "QGHWDMetalShaderSourceDefine.h"
  17. #import "QGHWDShaderTypes.h"
  18. #import "QGVAPLogger.h"
  19. @interface QGVAPMetalShaderFunctionLoader () {
  20. BOOL _alreadyLoadDefaultLibrary;
  21. BOOL _alreadyLoadHWDLibrary;
  22. }
  23. @property (nonatomic, strong) id<MTLDevice> device;
  24. @property (nonatomic, strong) id<MTLLibrary> defaultLibrary;
  25. @property (nonatomic, strong) id<MTLLibrary> hwdLibrary;
  26. @end
  27. @implementation QGVAPMetalShaderFunctionLoader
  28. - (instancetype)initWithDevice:(id<MTLDevice>)device {
  29. if (self = [super init]) {
  30. _device = device;
  31. }
  32. return self;
  33. }
  34. - (id<MTLFunction>)loadFunctionWithName:(NSString *)funcName {
  35. id<MTLFunction> program = nil;
  36. [self loadDefaultLibraryIfNeed];
  37. program = [self.defaultLibrary newFunctionWithName:funcName];
  38. //没有找到defaultLibrary文件 || defaultLibrary中不包含对应的fucntion
  39. if (!program) {
  40. [self loadHWDLibraryIfNeed];
  41. program = [self.hwdLibrary newFunctionWithName:funcName];
  42. }
  43. return program;
  44. }
  45. - (void)loadDefaultLibraryIfNeed {
  46. if (self.defaultLibrary || _alreadyLoadDefaultLibrary) {
  47. return ;
  48. }
  49. NSBundle *bundle = [NSBundle bundleForClass:self.class];
  50. NSString *metalLibPath = [bundle pathForResource:@"default" ofType:@"metallib"];
  51. if (metalLibPath.length == 0) {
  52. return ;
  53. }
  54. NSError *error = nil;
  55. id<MTLLibrary> defaultLibrary = [self.device newLibraryWithFile:metalLibPath error:&error];
  56. if (!defaultLibrary || error) {
  57. VAP_Error(kQGVAPModuleCommon, @"loadDefaultLibrary error!:%@", error);
  58. return ;
  59. }
  60. self.defaultLibrary = defaultLibrary;
  61. _alreadyLoadDefaultLibrary = YES;
  62. }
  63. - (void)loadHWDLibraryIfNeed {
  64. if (self.hwdLibrary || _alreadyLoadHWDLibrary) {
  65. return ;
  66. }
  67. NSError *error = nil;
  68. NSString *sourceString = [NSString stringWithFormat:@"%@%@%@", kQGHWDMetalShaderSourceImports, kQGHWDMetalShaderTypeDefines, kQGHWDMetalShaderSourceString];
  69. id<MTLLibrary> hwdLibrary = [self.device newLibraryWithSource:sourceString options:nil error:&error];
  70. if (!hwdLibrary || error) {
  71. VAP_Error(kQGVAPModuleCommon, @"loadHWDLibrary error!:%@", error);
  72. return ;
  73. }
  74. self.hwdLibrary = hwdLibrary;
  75. _alreadyLoadHWDLibrary = YES;
  76. }
  77. @end