CameraTextureRenderPass.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // CameraTextureRenderPass.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/26.
  6. //
  7. #import "CameraTextureRenderPass.h"
  8. #import <MetalKit/MetalKit.h>
  9. #import "Renderer.h"
  10. #import "Common.h"
  11. #import "TensorFlowWrapper.h"
  12. #import "PCSTools.h"
  13. @interface CameraTextureRenderPass ()
  14. @property (nonatomic, strong) UIImage* img;
  15. @property (nonatomic, assign) CVMetalTextureCacheRef textureCache;
  16. @property (nonatomic, strong) id<MTLRenderPipelineState> pipelineStateObject;
  17. @end
  18. @implementation CameraTextureRenderPass
  19. - (instancetype)init{
  20. self = [super init];
  21. if(self){
  22. CVReturn ret = CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, [Renderer shared].device, nil, &_textureCache);
  23. if(ret != kCVReturnSuccess){
  24. return nil;
  25. }
  26. id<MTLDevice> device = [Renderer shared].device;
  27. NSError* err = nil;
  28. id <MTLLibrary> library = [device newDefaultLibraryWithBundle:[PCSTools sdkBundle] error:&err];
  29. if(library == nil){
  30. return nil;
  31. }
  32. id <MTLFunction> vertFunc = [library newFunctionWithName:@"vert_main"];
  33. id <MTLFunction> fragFunc = [library newFunctionWithName:@"frag_main"];
  34. MTLRenderPipelineDescriptor* desc = [[MTLRenderPipelineDescriptor alloc] init];
  35. desc.label = @"camera";
  36. desc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
  37. desc.vertexFunction = vertFunc;
  38. desc.fragmentFunction = fragFunc;
  39. desc.sampleCount = 1;
  40. desc.depthAttachmentPixelFormat = MTLPixelFormatInvalid;
  41. _pipelineStateObject = [device newRenderPipelineStateWithDescriptor:desc error:nil];
  42. }
  43. return self;
  44. }
  45. //- (id<MTLTexture>)makeTextureWith: (CMSampleBufferRef)sampleBuffer{
  46. //
  47. // CVImageBufferRef imgBufRef = CMSampleBufferGetImageBuffer(sampleBuffer);
  48. // CVMetalTextureCacheRef textureCache = self.textureCache;
  49. //
  50. // if(imgBufRef != nil && textureCache != nil){
  51. //
  52. // size_t width = CVPixelBufferGetWidth(imgBufRef);
  53. // size_t height = CVPixelBufferGetHeight(imgBufRef);
  54. //
  55. // CVMetalTextureRef imageTexture = nil;
  56. // CVReturn result = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache, imgBufRef, nil, MTLPixelFormatBGRA8Unorm, width, height, 0, &imageTexture);
  57. // if(result != kCVReturnSuccess){
  58. // return nil;
  59. // }
  60. // id<MTLTexture> texture = CVMetalTextureGetTexture(imageTexture);
  61. // @synchronized (self) {
  62. // self.texture = texture;
  63. // }
  64. // CFRelease(imageTexture);
  65. //
  66. // return texture;
  67. // }
  68. // return nil;
  69. //}
  70. - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size{
  71. }
  72. - (void)renderIn:(MTKView *)view withCommandBuffer:(id<MTLCommandBuffer>)cmdBuf{
  73. MTLRenderPassDescriptor* renderPassDes = view.currentRenderPassDescriptor;
  74. if(renderPassDes == nil){
  75. return;
  76. }
  77. id <MTLRenderCommandEncoder> renderEncoder = [cmdBuf renderCommandEncoderWithDescriptor:view.currentRenderPassDescriptor];
  78. if(renderEncoder == nil){
  79. return;
  80. }
  81. [renderEncoder setRenderPipelineState:self.pipelineStateObject];
  82. @synchronized (self){
  83. [renderEncoder setFragmentTexture:self.texture atIndex:CameraCapturedTexture];
  84. }
  85. [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
  86. [renderEncoder endEncoding];
  87. }
  88. @end