// // CameraTextureRenderPass.m // LenzCameraNativeModuleForRN // // Created by 王昭威 on 2023/1/26. // #import "CameraTextureRenderPass.h" #import #import "Renderer.h" #import "PCSTools.h" @interface CameraTextureRenderPass () @property (nonatomic, strong) UIImage* img; @property (nonatomic, assign) CVMetalTextureCacheRef textureCache; @property (nonatomic, strong) id pipelineStateObject; @end @implementation CameraTextureRenderPass - (instancetype)init{ self = [super init]; if(self){ CVReturn ret = CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, [Renderer shared].device, nil, &_textureCache); if(ret != kCVReturnSuccess){ return nil; } id device = [Renderer shared].device; NSError* err = nil; id library = [device newDefaultLibraryWithBundle:[PCSTools sdkBundle] error:&err]; if(library == nil){ return nil; } id vertFunc = [library newFunctionWithName:@"vert_main"]; id fragFunc = [library newFunctionWithName:@"frag_main"]; MTLRenderPipelineDescriptor* desc = [[MTLRenderPipelineDescriptor alloc] init]; desc.label = @"camera"; desc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; desc.vertexFunction = vertFunc; desc.fragmentFunction = fragFunc; desc.sampleCount = 1; desc.depthAttachmentPixelFormat = MTLPixelFormatInvalid; _pipelineStateObject = [device newRenderPipelineStateWithDescriptor:desc error:nil]; } return self; } //- (id)makeTextureWith: (CMSampleBufferRef)sampleBuffer{ // // CVImageBufferRef imgBufRef = CMSampleBufferGetImageBuffer(sampleBuffer); // CVMetalTextureCacheRef textureCache = self.textureCache; // // if(imgBufRef != nil && textureCache != nil){ // // size_t width = CVPixelBufferGetWidth(imgBufRef); // size_t height = CVPixelBufferGetHeight(imgBufRef); // // CVMetalTextureRef imageTexture = nil; // CVReturn result = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache, imgBufRef, nil, MTLPixelFormatBGRA8Unorm, width, height, 0, &imageTexture); // if(result != kCVReturnSuccess){ // return nil; // } // id texture = CVMetalTextureGetTexture(imageTexture); // @synchronized (self) { // self.texture = texture; // } // CFRelease(imageTexture); // // return texture; // } // return nil; //} - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size{ } - (void)renderIn:(MTKView *)view withCommandBuffer:(id)cmdBuf{ MTLRenderPassDescriptor* renderPassDes = view.currentRenderPassDescriptor; if(renderPassDes == nil){ return; } id renderEncoder = [cmdBuf renderCommandEncoderWithDescriptor:view.currentRenderPassDescriptor]; if(renderEncoder == nil){ return; } [renderEncoder setRenderPipelineState:self.pipelineStateObject]; [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4]; [renderEncoder endEncoding]; } @end