iOS to Unity Metal紋理

將CVPixelBufferRef轉(zhuǎn)為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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容