Re: FSAA, Flashing Sample on iPhone....

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
JVene
Kobold
Posts: 25
Joined: Sat Jan 29, 2011 5:03 am

Re: FSAA, Flashing Sample on iPhone....

Post by JVene »

I'm starting a new direction on the subject of this thread, which I'll summarize: http://www.ogre3d.org/forums/viewtopic.php?f=2&t=62864

FSAA is off by default in the SampleBrowser application for 1.7.2, and when built for the iPhone device (in my case an iTouch 2g) for iOS 4.1, the display would flash alternating black/menu/black/menu (or, instead of black, whatever nonsense was left in the memory). This is related to the FSAA setting.

It took a few days to sort out by I built for iPhone (device, not simulator) from source. Early on the simulator worked fine, but the SampleBrowser application on the device would flash, and NOT animate the menu.

Masterfalcon noted a bug report relating to an Apple API ( glResolveMultisampleFramebufferAPPLE() ) in the function EAGLWindow::swapBuffers where FSAA is applied, but while that was VERY close, it wasn't the problem. In fact, as shipped, the default configuration for the samplebrowser has FSAA set to 0, so the code wasn't even being called.

Then, DanielSefton recognized the problem, suggested setting FSAA to 2, and that caused it to work.

That troubled me to think that non-FSAA display is broken, and I think it is - here's a proposed fix, and the true nature of my inquiry here.

In OgreEAGLWindow.mm, around line 411, this function appears.

Code: Select all

    void EAGLWindow::swapBuffers(bool waitForVSync)
    {
        if (mClosed || mIsExternalGLControl)
        {
            return;
        }
        
        if (mAnimationTimer->getMilliseconds() < kSwapInterval)
        {
            return;
        }

        mAnimationTimer->reset();

#if GL_APPLE_framebuffer_multisample
        if(mContext->mIsMultiSampleSupported && mContext->mNumSamples > 0)
        {
            glDisable(GL_SCISSOR_TEST);     
            glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, mContext->mFSAAFramebuffer);
            GL_CHECK_ERROR
            glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, mContext->mViewFramebuffer);
            GL_CHECK_ERROR
            glResolveMultisampleFramebufferAPPLE();
            GL_CHECK_ERROR
        }
/* **************************************************
    The following is my proposed solution, which works
    ************************************************** */
        else 
        {
            glBindFramebufferOES(GL_FRAMEBUFFER_OES, mContext->mViewFramebuffer);   
        }
#else
        glBindFramebufferOES(GL_FRAMEBUFFER_OES, mContext->mViewFramebuffer);    
#endif

#if GL_EXT_discard_framebuffer
        // Framebuffer discard is only supported on devices running iOS 4+
        if(mCurrentOSVersion >= 4.0)
        {
            GLenum attachments[] = { GL_COLOR_ATTACHMENT0_OES, GL_DEPTH_ATTACHMENT_OES, GL_STENCIL_ATTACHMENT_OES };
            glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 3, attachments);
            GL_CHECK_ERROR
        }
#endif

        glBindRenderbufferOES(GL_RENDERBUFFER_OES, mContext->mViewRenderbuffer);
        GL_CHECK_ERROR
        if ([mContext->getContext() presentRenderbuffer:GL_RENDERBUFFER_OES] == NO)
        {
            GL_CHECK_ERROR
            OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
                        "Failed to swap buffers in ",
                        __FUNCTION__);
        }
    }
I've been a developer for 30 years, but I'm new to Ogre and only fair at OpenGL, so I can't say with certainty that this proposed solution is correct, but it's a demonstration of the problem via a solution.

It seem to me that the reason when FSAA is on the device works is because of this:

Code: Select all

            glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, mContext->mViewFramebuffer);
That's around line 431. It's part of the application of FSAA, but it also leaves the framebuffer selected. Subsequently, the use of the render buffer, and then it's presentation, works fine.

However, in the original code, if FSAA is off, it seems to me that the framebuffer isn't selected, and as a result there's unpredictable results.

I find it odd that it works in simulator and not the device, but the simulator is actually running in OpenGL...the GLES API is simply mapped, and so behavior can be different. It might also be different on the various devices and OS versions, I don't know - I only have the iTouch device for testing at present.

What I appended was an "else"...if FSAA isn't applied (mNumSamples == 0), then a more standard call to bind the frame buffer is added, and since the entire FSAA processing is within a define, such that it MIGHT not even be compiled, I appended an else to that define with a repeat of the selection of the framebuffer, as in:

Code: Select all

#if GL_APPLE_framebuffer_multisample
        if(mContext->mIsMultiSampleSupported && mContext->mNumSamples > 0)
        {
            glDisable(GL_SCISSOR_TEST);     
            glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, mContext->mFSAAFramebuffer);
            GL_CHECK_ERROR
            glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, mContext->mViewFramebuffer);
            GL_CHECK_ERROR
            glResolveMultisampleFramebufferAPPLE();
            GL_CHECK_ERROR
        }
        else 
        {
            glBindFramebufferOES(GL_FRAMEBUFFER_OES, mContext->mViewFramebuffer);   
        }
#else
        glBindFramebufferOES(GL_FRAMEBUFFER_OES, mContext->mViewFramebuffer);    
#endif
The original code has neither else (code or define)....and with FSAA set to zero, this appended code causes the application to work correctly.



An I know I'm nitpicking here, because on the device the FSAA doesn't affect frame rates much, so having it on is likely a benefit worth having.

My point, however, is that I (and as a result likely many other qualified developers trying this out on the phone) would have to spend a few days figuring out why, and maybe giving up on Ogre (as I nearly did)....I reasoned that it wasn't likely a serious problem, just elusive at first.

I wouldn't have had the problem if FSAA were defaulted to 2, but that's not a solution.

I'd propose this code if I knew where or how, but I'm not studied on the engine design sufficiently to know of there's a reason my solution isn't correct...it might be something else that left the buffer unattended, where this function expected the framebuffer to be intact, and is intact under other platforms.

If the appropriate members already have an eye on this point, I'm happy to leave it there - and I'm curious to learn if I'm way off base, or close to correct.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: FSAA, Flashing Sample on iPhone....

Post by masterfalcon »

I've actually got a fix for this all ready to go. I'll try to commit it this week(it's a very busy one).
JVene
Kobold
Posts: 25
Joined: Sat Jan 29, 2011 5:03 am

Re: FSAA, Flashing Sample on iPhone....

Post by JVene »

:) Then the best pair of eyes are already upon the problem....

Genuinely, I feel better about choosing the engine already.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: FSAA, Flashing Sample on iPhone....

Post by masterfalcon »

One thing to note about my fix. I don't have a device with 4.1 on it to test, but I did see a fundamental problem with the code that could cause problems. As a preview, here's the new and improved swapBuffers function. If you'd want to try it out that would be super helpful.

Code: Select all

    void EAGLWindow::swapBuffers(bool waitForVSync)
    {
        if (mClosed || mIsExternalGLControl)
        {
            return;
        }

#if GL_APPLE_framebuffer_multisample
        if(mContext->mIsMultiSampleSupported && mContext->mNumSamples > 0)
        {
            glDisable(GL_SCISSOR_TEST);     
            glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, mContext->mFSAAFramebuffer);
            GL_CHECK_ERROR
            glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, mContext->mViewFramebuffer);
            GL_CHECK_ERROR
            glResolveMultisampleFramebufferAPPLE();
            GL_CHECK_ERROR

            GLenum attachments[] = { GL_COLOR_ATTACHMENT0_OES, GL_DEPTH_ATTACHMENT_OES, GL_STENCIL_ATTACHMENT_OES };
            glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 3, attachments);
            GL_CHECK_ERROR
            glBindFramebufferOES(GL_FRAMEBUFFER_OES, mContext->mViewFramebuffer);
            GL_CHECK_ERROR
        }
        else
        {
            GLenum attachments[] = { GL_COLOR_ATTACHMENT0_OES, GL_DEPTH_ATTACHMENT_OES };
            glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
            GL_CHECK_ERROR
        }
#endif

        glBindRenderbufferOES(GL_RENDERBUFFER_OES, mContext->mViewRenderbuffer);
        GL_CHECK_ERROR
        if ([mContext->getContext() presentRenderbuffer:GL_RENDERBUFFER_OES] == NO)
        {
            GL_CHECK_ERROR
            OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
                        "Failed to swap buffers in ",
                        __FUNCTION__);
        }
    }
JVene
Kobold
Posts: 25
Joined: Sat Jan 29, 2011 5:03 am

Re: FSAA, Flashing Sample on iPhone....

Post by JVene »

Oops, I spoke too soon.
That's a go! iTouch 2g on iOS 4.1, works with FSAA at 0 or 2, etc.

It even works if you manage to set GL_APPLE_framebuffer_multisample to 0
I must get used to the fact that Xcode can't always sense changes in source automatically, at least when the project is configured by CMake (the static libs, that is)...when I cleaned the project to create a release build, the release is doing as before.
I'll check further and post again later tonight on my study.
JVene
Kobold
Posts: 25
Joined: Sat Jan 29, 2011 5:03 am

Re: FSAA, Flashing Sample on iPhone....

Post by JVene »

Well, I'm sorry to report, the code as you've presented doesn't solve the problem when FSAA is turned off.

It's fine with FSAA on (the previous code was too), and though the behavior is slightly different, it's the same end result.

In the original 1.7.2 source, the 2g would show the opening splash, then the first 'frame' of the menu animation, without the sample thumbs, and alternate between that and a black display (or leftover buffer).

This time it flips between the empty menu and the first 'frame' of the menu animation ...effectively appearing as though the buttons and dialog controls are not flashing, but the thumbs are.

If I add a call to glBindFramebufferOES(.....) within the else clause, it works correctly.

If you happen to set GL_APPLE_framebuffer_multisample to 0 (in glext.h), then the 2g can't show the animation correctly - whatever corrective action binding the frame buffer is doing for the 2g isn't part of the compilation in that case.

I sense this is an PowerVR MBX behavior - perhaps it's not so on the SBX chip.

There's no doubt this works in the simulator, and I've check with the PowerVRFrame under Windows (a long story...) and this works there too, but those are emulating through entirely different hardware / drivers.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: FSAA, Flashing Sample on iPhone....

Post by masterfalcon »

Ahh, you're right. There is a buffer bind missing in there. I also think you're correct regarding MBX/SGX behavior. I've been testing lately on primarily SGX devices, there must be something different between the two device drivers.

Are you adding the bind before or after the framebuffer discard?
JVene
Kobold
Posts: 25
Joined: Sat Jan 29, 2011 5:03 am

Re: FSAA, Flashing Sample on iPhone....

Post by JVene »

I added it before the buffer discard, as it worked in the previous quick fix I posted earlier.
User avatar
jbuck
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 47
Joined: Mon Feb 14, 2011 12:45 pm
Location: San Diego, CA

Re: FSAA, Flashing Sample on iPhone....

Post by jbuck »

We recently put out an update of our game Autodesk TinkerBox using the code at commit 2671. We had reports from users with OS 3.x that it was crashing when launched. We were able to repro on our end and tracked it down to a change in EAGLWindow.mm. The glDiscardFramebufferEXT used to be wrapped with both a #if and an OS version check, but both of those were removed in commit 2668. I re-wrapped the discard buffer calls with those checks, and 3.x now works fine. According to this:

http://developer.apple.com/library/ios/ ... CH103-SW26

Under "Discard Unneeded Renderbuffers": "A discard operation is defined by the EXT_discard_framebuffer extension and is available on iOS 4.0 and later. Discard operations should be omitted when your application is running on earlier versions of ioS, but included whenever they are available."

So, wrapping that code was the correct thing; not sure why it got deleted, but hopefully you can re-incorporate that code back into the codebase (I'm using the 1.7.2 branch).


P.S. I posted in this thread since it appears that this thread is what initiated the change.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: FSAA, Flashing Sample on iPhone....

Post by masterfalcon »

Thanks so much! I don't remember why it got deleted. It was likely an oversight. I'll reinstate the check, since it should be there, and commit it right away.
User avatar
jbuck
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 47
Joined: Mon Feb 14, 2011 12:45 pm
Location: San Diego, CA

Re: FSAA, Flashing Sample on iPhone....

Post by jbuck »

Wow, that was fast. :)

I saw your change and noted you also wrapped the glBindFramebufferOES calls with the version check. I didn't do that in my test on my end but wonder if there's a benefit to doing that (or is it a bug)?
Last edited by jbuck on Sat Mar 12, 2011 8:07 am, edited 1 time in total.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: FSAA, Flashing Sample on iPhone....

Post by masterfalcon »

Looking at it again, I think it's actually a bug.
fanlansen
Gnoblar
Posts: 18
Joined: Tue Jun 29, 2010 9:28 am

Re: FSAA, Flashing Sample on iPhone....

Post by fanlansen »

1.7.3 and 1.8 has the problem too.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: FSAA, Flashing Sample on iPhone....

Post by masterfalcon »

Which hardware are you seeing it on?
fanlansen
Gnoblar
Posts: 18
Joined: Tue Jun 29, 2010 9:28 am

Re: FSAA, Flashing Sample on iPhone....

Post by fanlansen »

iOS 4.1 + iTouch 2nd + OpenGLES 1.x
I edit code like upon, then It work (1.8)
User avatar
metaldev
Orc Shaman
Posts: 761
Joined: Thu Mar 17, 2005 11:56 pm
Location: Boston
x 15

Re: FSAA, Flashing Sample on iPhone....

Post by metaldev »

Is this the same bug you guys are seeing in this thread?
(The video is mirrored due to the webcam software, but the app itself runs normally)

[youtube]nnguS4Fovng[/youtube]

taken on: iTouch 2G / iOS 4.1/ Ogre 1.7.3
User avatar
jbuck
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 47
Joined: Mon Feb 14, 2011 12:45 pm
Location: San Diego, CA

Re: FSAA, Flashing Sample on iPhone....

Post by jbuck »

Huh, I saw a similar issue some months back when we got TinkerBox running on iPhone after it was running fine on iPad. We had an odd double-buffer-looking flickering issue. The iPad version had the game at fullscreen with the UI dynamically coming on the screen. When I change the iPhone code so that the game ran in a sub-fullscreen viewport with the UI permanently affixed to the side of the screen, the flickering mysteriously disappeared. I never did figure out what was wrong, and it still works fine after a couple updates released for the app, but it "felt" like a subtle timing issue at the time.