还原metal的流程

This commit is contained in:
Wang.Renzhu 2022-08-03 14:41:40 +08:00
parent 673794bea7
commit c123dfae33
5 changed files with 5418 additions and 762 deletions

View File

@ -19,7 +19,7 @@
#include <math.h> #include <math.h>
#include "SourceCamera.hpp" #include "SourceCamera.hpp"
#include "Context.hpp" #include "Context.hpp"
#include "GPUImageUtil.h" #include "util.h"
#if defined(__APPLE__) #if defined(__APPLE__)
#include "CVFramebuffer.hpp" #include "CVFramebuffer.hpp"
#endif #endif
@ -81,7 +81,7 @@ void SourceCamera::setIORenderTexture(IOSurfaceID surfaceID,
GLuint texture, GLuint texture,
int width, int width,
int height, int height,
Opipe::RotationMode outputRotation, GPUImage::RotationMode outputRotation,
SourceType sourceType, SourceType sourceType,
TextureAttributes textureAttributes) { TextureAttributes textureAttributes) {
//纹理发生变化使用新的framebuffer //纹理发生变化使用新的framebuffer
@ -165,15 +165,11 @@ void SourceCamera::setFrameData(int width,
this->setFramebuffer(framebuffer, outputRotation); this->setFramebuffer(framebuffer, outputRotation);
CHECK_GL(glBindTexture(GL_TEXTURE_2D, this->getFramebuffer()->getTexture())); CHECK_GL(glBindTexture(GL_TEXTURE_2D, this->getFramebuffer()->getTexture()));
float offset = 1.0;
#ifdef VERSION_LIMIT
offset = 1 - (texture * M_PI - floor(texture * M_PI));
#endif
switch (sourceType) { switch (sourceType) {
case SourceType_RGBA: case SourceType_RGBA:
if (pixels) { if (pixels) {
CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width * offset, height * offset, 0, pixelsType, GL_UNSIGNED_BYTE, pixels)); CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, pixelsType, GL_UNSIGNED_BYTE, pixels));
} }
break; break;
case SourceType_YUV420SP: case SourceType_YUV420SP:
@ -182,7 +178,7 @@ void SourceCamera::setFrameData(int width,
_UVFrameBuffer = getContext()->getFramebufferCache()->fetchFramebuffer(_context, width * 0.5, height * 0.5, true); _UVFrameBuffer = getContext()->getFramebufferCache()->fetchFramebuffer(_context, width * 0.5, height * 0.5, true);
CHECK_GL(glBindTexture(GL_TEXTURE_2D, _UVFrameBuffer->getTexture())); CHECK_GL(glBindTexture(GL_TEXTURE_2D, _UVFrameBuffer->getTexture()));
CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width * 0.5 * offset, height * 0.5 * offset, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, upixels)); CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width * 0.5, height * 0.5, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, upixels));
} }
break; break;
case SourceType_YUV420P: case SourceType_YUV420P:
@ -193,10 +189,10 @@ void SourceCamera::setFrameData(int width,
_UVFrameBuffer = getContext()->getFramebufferCache()->fetchFramebuffer(_context, w, h, true); _UVFrameBuffer = getContext()->getFramebufferCache()->fetchFramebuffer(_context, w, h, true);
CHECK_GL(glBindTexture(GL_TEXTURE_2D, _UVFrameBuffer->getTexture())); CHECK_GL(glBindTexture(GL_TEXTURE_2D, _UVFrameBuffer->getTexture()));
CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w * offset, h * offset, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, upixels)); CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, upixels));
_VFrameBuffer = getContext()->getFramebufferCache()->fetchFramebuffer(_context, w, h, true); _VFrameBuffer = getContext()->getFramebufferCache()->fetchFramebuffer(_context, w, h, true);
CHECK_GL(glBindTexture(GL_TEXTURE_2D, _VFrameBuffer->getTexture())); CHECK_GL(glBindTexture(GL_TEXTURE_2D, _VFrameBuffer->getTexture()));
CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w * offset, h * offset, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, vpixels)); CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, vpixels));
} }
break; break;
default: default:

View File

@ -46,17 +46,17 @@ public:
virtual void setRenderTexture(GLuint texture, virtual void setRenderTexture(GLuint texture,
int width, int width,
int height, int height,
Opipe::RotationMode outputRotation = Opipe::RotationMode::NoRotation, GPUImage::RotationMode outputRotation = GPUImage::RotationMode::NoRotation,
SourceType sourceType = SourceType_RGBA, SourceType sourceType = SourceType_RGBA,
const Opipe::TextureAttributes textureAttributes = Opipe::Framebuffer::defaultTextureAttribures); const GPUImage::TextureAttributes textureAttributes = GPUImage::Framebuffer::defaultTextureAttribures);
#if defined(__APPLE__) #if defined(__APPLE__)
virtual void setIORenderTexture(IOSurfaceID surfaceID, virtual void setIORenderTexture(IOSurfaceID surfaceID,
GLuint texture, GLuint texture,
int width, int width,
int height, int height,
Opipe::RotationMode outputRotation = Opipe::RotationMode::NoRotation, GPUImage::RotationMode outputRotation = GPUImage::RotationMode::NoRotation,
SourceType sourceType = SourceType_RGBA, SourceType sourceType = SourceType_RGBA,
const Opipe::TextureAttributes textureAttributes = Opipe::Framebuffer::defaultTextureAttribures); const GPUImage::TextureAttributes textureAttributes = GPUImage::Framebuffer::defaultTextureAttribures);
#endif #endif

View File

@ -55,7 +55,6 @@
/// 原始相机纹理 可以快速读取 /// 原始相机纹理 可以快速读取
@property (nonatomic, readonly, strong) OlaShareTexture *cameraTexture; @property (nonatomic, readonly, strong) OlaShareTexture *cameraTexture;
@property (nonatomic, readonly, strong) OlaShareTexture *halfCameraTexture; @property (nonatomic, readonly, strong) OlaShareTexture *halfCameraTexture;
@property (nonatomic, readonly, strong) OlaShareTexture *shareTexture;
/// 不带后处理的相机渲染的原始纹理 /// 不带后处理的相机渲染的原始纹理
@property (nonatomic, readonly) CVPixelBufferRef renderTarget; @property (nonatomic, readonly) CVPixelBufferRef renderTarget;

View File

@ -28,7 +28,6 @@ NS_INLINE size_t QAAlignSize(size_t size)
@property (nonatomic, strong) OlaMTLCameraRender *mtlRender; @property (nonatomic, strong) OlaMTLCameraRender *mtlRender;
@property (nonatomic) NSTimeInterval frameTime; @property (nonatomic) NSTimeInterval frameTime;
@property (nonatomic, strong) OlaShareTexture *shareTexture;
@property (nonatomic, strong) OlaShareTexture *cameraTexture; @property (nonatomic, strong) OlaShareTexture *cameraTexture;
@property (nonatomic) id<MTLTexture> ioSurfaceTexture; @property (nonatomic) id<MTLTexture> ioSurfaceTexture;
@property (nonatomic) IOSurfaceID lastIOSurfaceID; @property (nonatomic) IOSurfaceID lastIOSurfaceID;
@ -50,7 +49,6 @@ NS_INLINE size_t QAAlignSize(size_t size)
_openGLContext = nil; _openGLContext = nil;
_mtlRender = nil; _mtlRender = nil;
_shareTexture = nil;
_cameraTexture = nil; _cameraTexture = nil;
_halfCameraTexture = nil; _halfCameraTexture = nil;
} }
@ -93,12 +91,6 @@ NS_INLINE size_t QAAlignSize(size_t size)
CGSize textureSize = CGSizeMake(float(alighWidth), CGSize textureSize = CGSizeMake(float(alighWidth),
frame.size.height * self.contentScaleFactor); frame.size.height * self.contentScaleFactor);
_shareTexture = [[OlaShareTexture alloc] initWithMetalDevice:self.device
openGLContext:self.openGLContext
metalPixelFormat:self.colorPixelFormat
size:textureSize];
_cameraTexture = [[OlaShareTexture alloc] initWithMetalDevice:self.device _cameraTexture = [[OlaShareTexture alloc] initWithMetalDevice:self.device
openGLContext:self.openGLContext openGLContext:self.openGLContext
metalPixelFormat:self.colorPixelFormat metalPixelFormat:self.colorPixelFormat
@ -119,7 +111,7 @@ NS_INLINE size_t QAAlignSize(size_t size)
dispatch_queue_attr_t interactive = dispatch_queue_attr_t interactive =
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
QOS_CLASS_USER_INTERACTIVE, 0); QOS_CLASS_DEFAULT, 0);
self.displayFrameRenderingSemaphore = dispatch_semaphore_create(MaxFramesInFlight); self.displayFrameRenderingSemaphore = dispatch_semaphore_create(MaxFramesInFlight);
self.displayRenderQueue = dispatch_queue_create("Ola.ios.displayRenderQueue", self.displayRenderQueue = dispatch_queue_create("Ola.ios.displayRenderQueue",
interactive); interactive);
@ -188,11 +180,7 @@ NS_INLINE size_t QAAlignSize(size_t size)
// dispatch_semaphore_t block_camera_sema = self.cameraFrameRenderingSemaphore; // dispatch_semaphore_t block_camera_sema = self.cameraFrameRenderingSemaphore;
dispatch_semaphore_t block_display_sema = self.displayFrameRenderingSemaphore; dispatch_semaphore_t block_display_sema = self.displayFrameRenderingSemaphore;
void (^renderCompleted)(id<MTLCommandBuffer> buffer) = ^(id<MTLCommandBuffer> buffer)
{
// dispatch_semaphore_signal(block_camera_sema);
dispatch_semaphore_signal(block_display_sema);
};
NSMutableArray<OlaCameraRender *> *renders = [self.renders copy]; NSMutableArray<OlaCameraRender *> *renders = [self.renders copy];
@ -201,20 +189,25 @@ NS_INLINE size_t QAAlignSize(size_t size)
return; return;
} }
void (^renderCompleted)(id<MTLCommandBuffer> buffer) = ^(id<MTLCommandBuffer> buffer)
{
// dispatch_semaphore_signal(block_camera_sema);
dispatch_semaphore_signal(block_display_sema);
};
__strong OlaMTLCameraRenderView *strongSelf = weakSelf; __strong OlaMTLCameraRenderView *strongSelf = weakSelf;
strongSelf.frameTime += (1.0 / strongSelf.preferredFramesPerSecond) * 1000.0; strongSelf.frameTime += (1.0 / strongSelf.preferredFramesPerSecond) * 1000.0;
if (dispatch_semaphore_wait(block_display_sema, DISPATCH_TIME_NOW) != 0) if (dispatch_semaphore_wait(block_display_sema, DISPATCH_TIME_NOW) != 0)
{ {
NSLog(@"丢帧了:%f", strongSelf.frameTime);
return; return;
} }
id<MTLCommandBuffer> commandBuffer = nil; id<MTLCommandBuffer> commandBuffer = nil;
commandBuffer = [strongSelf.mtlRender.commandQueue commandBuffer]; commandBuffer = [strongSelf.mtlRender.commandQueue commandBuffer];
[strongSelf.mtlRender renderToShareTexture:strongSelf.shareTexture.metalTexture
commandBuffer:commandBuffer
frameTime:strongSelf.frameTime];
[strongSelf.mtlRender renderToTexture:strongSelf.halfCameraTexture.metalTexture [strongSelf.mtlRender renderToTexture:strongSelf.halfCameraTexture.metalTexture
from:strongSelf.cameraTexture.metalTexture commandBuffer:commandBuffer from:strongSelf.cameraTexture.metalTexture commandBuffer:commandBuffer
textureCoordinate:strongSelf.mtlRender.noRotationBuffer]; textureCoordinate:strongSelf.mtlRender.noRotationBuffer];
@ -240,16 +233,18 @@ NS_INLINE size_t QAAlignSize(size_t size)
if (strongSelf.cameraDelegate && !strongSelf.isPaused) { if (strongSelf.cameraDelegate && !strongSelf.isPaused) {
if (@available(iOS 11.0, *)) { if (@available(iOS 11.0, *)) {
glFlush();
[EAGLContext setCurrentContext:strongSelf.openGLContext]; [EAGLContext setCurrentContext:strongSelf.openGLContext];
[strongSelf.cameraDelegate draw:strongSelf.frameTime]; [strongSelf.cameraDelegate draw:strongSelf.frameTime];
glFlush();
IOSurfaceID surfaceId = [strongSelf.cameraDelegate externalRender:strongSelf.frameTime IOSurfaceID surfaceId = [strongSelf.cameraDelegate externalRender:strongSelf.frameTime
targetTexture:strongSelf.cameraTexture targetTexture:strongSelf.cameraTexture
commandBuffer:commandBuffer]; commandBuffer:commandBuffer];
[strongSelf.cameraDelegate bgraCameraTextureReady:strongSelf.halfCameraTexture [strongSelf.cameraDelegate bgraCameraTextureReady:strongSelf.halfCameraTexture
onScreenTexture:strongSelf.shareTexture onScreenTexture:strongSelf.cameraTexture
frameTime:strongSelf.frameTime * 1000]; frameTime:strongSelf.frameTime * 1000];
if (surfaceId != -1) { if (surfaceId != -1) {
//这里渲染surfaceId //这里渲染surfaceId
@ -293,11 +288,8 @@ NS_INLINE size_t QAAlignSize(size_t size)
} }
} }
glFlush();
//将混合渲染结果 渲染到屏幕
[strongSelf.mtlRender renderToTexture:drawable.texture [strongSelf.mtlRender renderToTexture:drawable.texture
from:strongSelf.shareTexture.metalTexture from:strongSelf.cameraTexture.metalTexture
commandBuffer:commandBuffer commandBuffer:commandBuffer
textureCoordinate:strongSelf.mtlRender.noRotationBuffer]; textureCoordinate:strongSelf.mtlRender.noRotationBuffer];
if (drawable) { if (drawable) {
@ -383,7 +375,7 @@ NS_INLINE size_t QAAlignSize(size_t size)
{ {
NSAssert([NSThread isMainThread], @"call on main Thread"); NSAssert([NSThread isMainThread], @"call on main Thread");
[render setupWithDevice:self.device shareTexture:self.shareTexture useRenderMode:self.useRenderMode]; [render setupWithDevice:self.device shareTexture:self.cameraTexture useRenderMode:self.useRenderMode];
[self.renders addObject:render]; [self.renders addObject:render];
} }