无法使用 SceneKit 从 OpenGL 获取 CVPixelBufferRef 数据

Cannot get CVPixelBufferRef data from OpenGL with SceneKit

我需要使用 SceneKit 从 OpenGL 上下文中获取 CVPixelBufferRef。

正在创建渲染器

_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

_renderer = [SCNRenderer rendererWithContext:_context options:nil];
_renderer.delegate = self;
_renderer.scene = scene;

然后从 OpenGL 获取缓冲区

CVPixelBufferRef _targetBuffer;

GLuint frameBuffer = 0;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);

GLuint colorBuffer = 0;
glGenRenderbuffers(1, &colorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, _renderSize.width, _renderSize.height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);

GLuint depthBuffer = 0;
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

glClearColor(0.0, 0.0, 0.0, 0.0);

glViewport(0, 0, _renderSize.width, _renderSize.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

[_renderer renderAtTime:0];

glReadPixels(0, 0, _renderSize.width, _renderSize.height, GL_RGBA, GL_UNSIGNED_BYTE, _targetBuffer);

但是在渲染时出现错误:

[SceneKit] 错误:渲染 SceneKit 内容之前的 glError:506

正确设置像素和深度缓冲区:

glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);

glGenFramebuffers(1, &_frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, CVOpenGLESTextureGetName(_texture), 0);

glGenRenderbuffers(1, &_depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, _renderSize.width, _renderSize.height);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthBuffer);