【问题标题】:Huge drop in fps with transparent EAGLView透明 EAGLView 的 fps 大幅下降
【发布时间】:2013-01-28 22:36:37
【问题描述】:

我有一个 Cocos2d 项目,我想在整个应用程序中保持不变的背景。在其委托的applicationDidFinishLaunching 方法中,我替换了以下行:

我已将 glView 的 pixelFormat 从 kEAGLColorFormatRGB565 更改为 kEAGLColorFormatRGBA8。当我进行更改时,glView 变得透明并且我可以看穿它,但是 fps 急剧下降。如果我不进行更改,则视图不会变得透明,但我看不到 fps 的大幅下降。我说的是 fps 显着下降,从 59.0-60.0 下降到大约 35.0-42.0。

我在上面的 addSubview 行下方使用此代码来使视图透明:

director.openGLView.opaque = NO;

整个 applicationDidFinishLaunching 方法如下所示:

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    // Init the window
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Try to use CADisplayLink director
    // if it fails (SDK < 3.1) use the default director
    if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
        [CCDirector setDirectorType:kCCDirectorTypeDefault];


    CCDirector *director = [CCDirector sharedDirector];

    // Init the View Controller
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;

    //
    // Create the EAGLView manually
    //  1. Create a RGB565 format. Alternative: RGBA8
    //  2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
    //
    //
    EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
                                   pixelFormat:kEAGLColorFormatRGBA8    // kEAGLColorFormatRGBA8
                                   depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                        ];

    // attach the openglView to the director
    [director setOpenGLView:glView];
    glView.opaque = NO;
//  // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
    if( ! [director enableRetinaDisplay:YES] )
        CCLOG(@"Retina Display Not supported");

    //
    // VERY IMPORTANT:
    // If the rotation is going to be controlled by a UIViewController
    // then the device orientation should be "Portrait".
    //
    // IMPORTANT:
    // By default, this template only supports Landscape orientations.
    // Edit the RootViewController.m file to edit the supported orientations.
    //
#if GAME_AUTOROTATION == kGameAutorotationUIViewController 
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif

    [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:YES];


    // make the OpenGLView a child of the view controller
    [viewController setView:glView];

    //Required in iOS6, recommended in 4 and 5
    [window setRootViewController:viewController];

    // make the View Controller a child of the main window, needed for iOS 4 and 5
    [window addSubview: viewController.view];

    [window makeKeyAndVisible];

    // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
    // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
    // You can change anytime.
    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];


    // Removes the startup flicker
    [self removeStartupFlicker];

    // Run the intro Scene
    [[CCDirector sharedDirector] runWithScene: [Intro scene]];
}

关于为什么会发生这种情况的任何想法?如果需要,我可以提供更多代码。

我忘了提及我所做的另一项代码更改。在 CCDirector.m setGLDefaultValues我把这一行改成了:

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

到这里:

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

【问题讨论】:

    标签: opengl-es cocos2d-iphone transparency eaglview pixelformat


    【解决方案1】:

    每当您添加具有透明度的 Alpha 通道时,OpenGL 引擎都必须执行某种形式(尽管是默认设置)的 Alpha 混合。这是在非 RGBA(即 RGB 565)情况下不必执行的每个像素的数学运算。每当您添加引擎所需的数学运算时,帧速率都会下降。

    【讨论】:

    • 是否有另一种方法可以让 EAGLView 在不更改那行代码的情况下变得透明?
    • @Stephen - 有点取决于你想要做什么。可能存在 EAGLView 可以在后面而不是在前面(您当前的操作方式)的情况,这将允许 EAGLView 不透明。我假设你在它背后有一些你希望通过它展示的东西。如果你把后面的东西变成前面的东西,反之亦然,那么可能会有选择!除此之外并非如此,如果您需要通过另一件事显示某些内容,则需要允许透明度和混合。您也许可以更改混合模式。
    • @Stephen - 如果您知道您只会 100% 透明或 100% 不透明,您可以将混合模式修改为更简单(相对于更繁重的数学)的混合模式.它仍然允许透明,但它基本上能够更简单地打开或关闭。我知道在我编写的一个应用程序中,我有一个更简单地由 50% 点触发的功能。在这里试试:opengl.org/sdk/docs/man/xhtml/glBlendFunc.xml
    • 没有理由不能,您可能会意识到切换该上下文的时间滞后,但将帧速率提高到您想要/需要的位置可能是值得的。
    • 我可能会在 appDelegate 中编写一个自定义例程来切换模式,然后让其他子类调用它。换句话说,让代理做它应该做的事情并管理 EGL。
    【解决方案2】:

    这是意料之中的。 RGBA8888 帧缓冲区使用两倍的内存,GPU 需要执行更多工作才能渲染到此帧缓冲区中。这就是为什么 RGB565 是默认格式的原因。

    【讨论】:

    • 实际上它更多的是添加的数学然后它是添加的内存。正是添加的 alpha 混合使帧速率下降。换句话说,它不是渲染到更大的帧缓冲区,而是它在到达帧缓冲区之前必须做的数学运算,尽管我想你是对的,它还必须将 2 个额外的字节推送到帧缓冲区每个像素也是 :-) 也完全同意这是意料之中的 LOL
    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多