iOS to Unity Metal纹理

将CVPixelBufferRef转为Metal 纹理传给unity渲染

- (void)onRemodeVideoRenderArrival:(CVPixelBufferRef)pixelBuffer userID:(NSInteger)userID {

    CVPixelBufferLockBaseAddress(pixelBuffer, 0);

    {

        size_twidth =CVPixelBufferGetWidthOfPlane(pixelBuffer,0);

        size_theight =CVPixelBufferGetHeightOfPlane(pixelBuffer,0);

        MTLPixelFormatpixelFormat =MTLPixelFormatR8Unorm;// 这里的颜色格式不是RGBA


        MTLTextureDescriptor* txDesc = [[MTLTextureDescriptor alloc] init];

        txDesc.textureType = MTLTextureType2D;

        txDesc.height= height;

        txDesc.width= width;

        txDesc.depth=1;

        txDesc.pixelFormat= pixelFormat;

        txDesc.arrayLength=1;

        txDesc.mipmapLevelCount=1;

        void *buf = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);

        size_tbytesPerRow =CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer,0);

        _util->yTex= [_util->devicenewTextureWithDescriptor:txDesc];

        [_util->yTex replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:buf bytesPerRow:bytesPerRow];

        _util->yTexWidth= (int32_t)width;

        _util->yTexHeight= (int32_t)height;

//        void *yRet = (__bridge void*)(_util->yTex);

//        self.yVideoArrivalCallback(_host, yRet, width, height, userID);


    }

     {

        size_twidth =CVPixelBufferGetWidthOfPlane(pixelBuffer,1);

        size_theight =CVPixelBufferGetHeightOfPlane(pixelBuffer,1);


        MTLPixelFormatpixelFormat =MTLPixelFormatRG8Unorm;// 2-8bit的格式

        MTLTextureDescriptor* txDesc = [[MTLTextureDescriptor alloc] init];

        txDesc.textureType = MTLTextureType2D;

        txDesc.height= height;

        txDesc.width= width;

        txDesc.depth=1;

        txDesc.pixelFormat= pixelFormat;

        txDesc.arrayLength=1;

        txDesc.mipmapLevelCount=1;

        void *buf = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);

        size_tbytesPerRow =CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer,1);

        _util->uvTex= [_util->devicenewTextureWithDescriptor:txDesc];

        [_util->uvTex replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:buf bytesPerRow:bytesPerRow];

        _util->uvTexWidth= (int32_t)width;

        _util->uvTexHeight= (int32_t)height;

    }

    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

    _util->frame_updated = true;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。