image - 从 NSImage 转换 glTexture 的最有效方法?

标签 image cocoa opengl nsimage nsimagerep

(更新如下...)

OpenGL Cocoa/OSX 桌面应用程序面向 10.8+

我遇到这样的情况:每帧接收一次 NSImage 对象,并希望将其转换为 openGL 纹理(它是来自 IPCamera 的视频帧)。从SO和互联网上,我能找到的将 NSImage 转换为 glTexture 的最有用的实用方法是下面的代码。

虽然这对于偶尔(IE 加载)的情况来说很好,但它会消耗大量的性能,并且每帧运行一次会很糟糕。我已经分析了代码并将瓶颈缩小到两个调用。这些调用加在一起几乎占了整个应用程序运行时间的三分之二。

  1. 位图准备创建

     NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithData:[inputImage TIFFRepresentation]];
    

    认为问题可能出在我获取位图的方式上,并且因为我听说过 TIFFRepresentation 的性能方面的不好的事情,所以我尝试了这个,这确实更快,但只有一点点,并且还引入了一些奇怪的东西变色(一切看起来都是红色的):

    NSRect rect = NSMakeRect(0, 0, inputImage.size.width, inputImage.size.height);
    CGImageRef cgImage = [inputImage CGImageForProposedRect:&rect context:[NSGraphicsContext currentContext] hints:nil];
    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
    
  2. glTexImage2D 调用(我不知道如何改进。)

如何使下面的方法更有效?

或者,告诉我我做错了,我应该看看其他地方?这些帧以 MJPEG 格式传入,因此我可以在将 NSData 转换为 NSImage 之前使用它。然而,它是 jpeg 编码的,所以我必须处理它。我实际上还想要 NSImage 对象作为应用程序的另一部分。

-(void)loadNSImage:(NSImage*)inputImage intoTexture:(GLuint)glTexture{


    // If we are passed an empty image, just quit
    if (inputImage == nil){
        //NSLog(@"LOADTEXTUREFROMNSIMAGE: Error: you called me with an empty image!");
        return;
    }


    // We need to save and restore the pixel state
    [self GLpushPixelState];
    glEnable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, glTexture);


    //  Aquire and flip the data
    NSSize imageSize = inputImage.size;
    if (![inputImage isFlipped]) {
        NSImage *drawImage = [[NSImage alloc] initWithSize:imageSize];
        NSAffineTransform *transform = [NSAffineTransform transform];

        [drawImage lockFocus];

        [transform translateXBy:0 yBy:imageSize.height];
        [transform scaleXBy:1 yBy:-1];
        [transform concat];

        [inputImage drawAtPoint:NSZeroPoint
                                fromRect:(NSRect){NSZeroPoint, imageSize}
                               operation:NSCompositeCopy
                                fraction:1];

        [drawImage unlockFocus];

        inputImage = drawImage;
    }

    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithData:[inputImage TIFFRepresentation]];


    //  Now make a texture out of the bitmap data
    // Set proper unpacking row length for bitmap.
    glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)[bitmap pixelsWide]);

    // Set byte aligned unpacking (needed for 3 byte per pixel bitmaps).
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    NSInteger samplesPerPixel = [bitmap samplesPerPixel];

    // Nonplanar, RGB 24 bit bitmap, or RGBA 32 bit bitmap.
    if(![bitmap isPlanar] && (samplesPerPixel == 3 || samplesPerPixel == 4)) {

        // Create one OpenGL texture
        // FIXME: Very slow
        glTexImage2D(GL_TEXTURE_2D, 0,
                     GL_RGBA,//samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
                     (GLint)[bitmap pixelsWide],
                     (GLint)[bitmap pixelsHigh],
                     0,
                     GL_RGBA,//samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
                     GL_UNSIGNED_BYTE,
                     [bitmap bitmapData]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    }else{
        [[NSException exceptionWithName:@"ImageFormat" reason:@"Unsupported image format" userInfo:nil] raise];
    }
    [self GLpopPixelState];
}

个人资料屏幕截图:

enter image description here

更新

根据下面 Brad 的评论,我决定考虑简单地绕过 NSImage。在我的特定情况下,我能够在将 JPG 数据转换为 NSImage 之前将其作为 NSData 对象访问,因此效果很好:

  [NSBitmapImageRep imageRepWithData: imgData];

使用与上面大致相同的方法,但直接从位图代表开始,CPU 使用率从 80% 下降到 20%,我对速度感到满意。我有一个适合我的 ap 的解决方案。

我仍然想知道我原来的问题是否有答案,或者最好将其作为要避免的事情的实例教训。最后,我仍然想知道是否可以改善 glTexImage2D 调用的加载时间 - 尽管现在已经在合理范围内,但它仍然占用了该方法 99% 的负载(但也许这没关系。)

==========

最佳答案

根据数据的传入方式,您可以使用像素缓冲区对象 (PBO),它将在上传纹理时使用 DMA(避免 CPU),而将使用 memcpy 复制随机指针(使用 CPU )。

Apple 在此描述如何执行此操作:https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/opengl-macprogguide/opengl_texturedata/opengl_texturedata.html

基本上,PBO 为您提供了 OpenGL 可以 DMA 的内存块,因此使用它来分配将解压缩帧复制到其中的内存,然后通过绑定(bind)缓冲区并以 nil 作为内存引用调用 glTexSubImage2D 来进行绘制。

这确实依赖于您能够将传入数据直接传入 PBO - 否则您仍然需要 memcpy 到 PBO,从而失去它可能带来的任何好处。

关于image - 从 NSImage 转换 glTexture 的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22761467/

相关文章:

qt - 如何在 Qt 5 中使用带有实例化的 VAO

html - 使用 <picture> 元素完全隐藏图像

Cocoa:检查两个 NSArray 是否相等

ios - 使用 Swift 处理触控板事件

objective-c - 一般的NSPasteboard能占用多少内存?

c - 将 3d 模型文件(3ds/obj/任何开放格式)转换为 C 数组的工具

user-interface - OpenGL 3D GUI 引擎和/或 Assets

HTML+CSS : How to add shadow (as image) to image?

c# - 如何获取图像中透明度的坐标

HTML CSS 使背景图像静态化