CVOpenGLESTextureCacheCreateTextureFromImage instead of glRe



整整三天来,我一直在努力提高基于glReadPixels的AVAssetWriter的性能。我已经浏览了苹果的RosyWriter和Camera Ripple代码,以及Brad Larson的GPUImage,但我仍然在挠头。我也一直在尝试使用这些链接中的实现:

渲染-对-文本-带-ios-5-文本-缓存-api

更快的替代方案-智能手机中的像素-打开-es-2-0

还有很多,但无论我做什么,我都无法让它发挥作用。要么视频最终没有被处理,要么它变成黑色,要么我出现了各种错误。我不会在这里经历所有的。

为了简化我的问题,我想我应该专注于从屏幕上的openGL预览FBO中获取一个快照。如果我只能让一个单独的实现工作,我应该能够解决其余的问题。我从上面的第一个链接尝试了实现,看起来像这样:

CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, [glView context], 
NULL, &texCacheRef);
CFDictionaryRef empty = CFDictionaryCreate(kCFAllocatorDefault,
NULL,
NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFMutableDictionaryRef attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrs,
kCVPixelBufferIOSurfacePropertiesKey,
empty);
CVPixelBufferRef renderTarget = NULL;
CVPixelBufferCreate(kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_32BGRA,
attrs,
&renderTarget);
CVOpenGLESTextureRef renderTexture;
CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault,
texCacheRef,
renderTarget,
NULL,
GL_TEXTURE_2D,
GL_RGBA,
width,
height,
GL_BGRA,
GL_UNSIGNED_BYTE,
0,
&renderTexture);
CFRelease(attrs);
CFRelease(empty);
glBindTexture(CVOpenGLESTextureGetTarget(renderTexture), CVOpenGLESTextureGetName(renderTexture));
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLuint renderFrameBuffer;
glGenRenderbuffers(1, &renderFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, renderFrameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, CVOpenGLESTextureGetName(renderTexture), 0);
//Is this really how I pull pixels off my context?
CVPixelBufferLockBaseAddress(renderTarget, 0);
buffer = (GLubyte *)CVPixelBufferGetBaseAddress(renderTarget);
CVPixelBufferUnlockBaseAddress(renderTarget, 0);

这里到底应该发生什么?我的缓冲区最终是一堆零,所以我想我需要做一些额外的事情来从上下文中提取像素。。。或者我错过了什么?

我想要实现的只是一个与我今天使用的更快的等价物:

int pixelsCount = w * h;
buffer = (GLubyte *) malloc(pixelsCount * 4);
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
正如Brad所指出的,我误解了这个概念,没有进行任何实际的渲染。当我添加它时效果很好。

最新更新