CameraTextureRenderPass.m 3.2 KB

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