Renderer.m 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // Renderer.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/26.
  6. //
  7. #import "Renderer.h"
  8. #import "CameraTextureRenderPass.h"
  9. @interface Renderer ()
  10. @property (nonatomic, strong) id<MTLCommandQueue> commandQueue;
  11. @property (nonatomic, strong) CameraTextureRenderPass* cameraPass;
  12. @end
  13. @implementation Renderer
  14. + (instancetype)shared{
  15. static dispatch_once_t token;
  16. static Renderer* instance = nil;
  17. dispatch_once(&token, ^{
  18. instance = [[self alloc] init];
  19. });
  20. return instance;
  21. }
  22. - (void)drawInMTKView:(MTKView *)view{
  23. if(self.commandQueue == nil || view.currentDrawable == nil){
  24. return;
  25. }
  26. id<MTLCommandBuffer> cmdBuf = [self.commandQueue commandBuffer];
  27. [self.cameraPass renderIn:view withCommandBuffer: cmdBuf];
  28. [cmdBuf presentDrawable:view.currentDrawable];
  29. [cmdBuf commit];
  30. }
  31. - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size{
  32. }
  33. - (void)setup{
  34. self.cameraPass = [[CameraTextureRenderPass alloc] init];
  35. }
  36. - (instancetype)init{
  37. self = [super init];
  38. if(self){
  39. _device = MTLCreateSystemDefaultDevice();
  40. _commandQueue = [_device newCommandQueue];
  41. }
  42. return self;
  43. }
  44. @end