CameraTextureRenderPass.m 3.3 KB

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