flashing problem with samples on Metal in macOS Ventura Topic is solved

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


jwwalker
Goblin
Posts: 268
Joined: Thu Aug 12, 2021 10:06 pm
Location: San Diego, CA, USA
x 19

Re: new problem with Sample_PlanarReflections on Mac/Metal

Post by jwwalker »

dark_sylinc wrote: Thu Feb 16, 2023 10:57 pm

BTW this is an extreme long shot but it is worth a try: Does the problem go away if you set CMake variable OGRE_RESTRICT_ALIASING to OFF and change # define DECL_MALLOC __attribute__( ( malloc ) ) from OgrePlatform.h to # define DECL_MALLOC ?

It wouldn't be the first time that a compiler became better at optimizing, and turns out it revealed a bug in our code (it's basically a very aggressive optimization).

I doubt this is the case because this shouldn't affect debug builds, but it's not entirely impossible.

No change.

jwwalker
Goblin
Posts: 268
Joined: Thu Aug 12, 2021 10:06 pm
Location: San Diego, CA, USA
x 19

Re: new problem with Sample_PlanarReflections on Mac/Metal

Post by jwwalker »

dark_sylinc wrote: Thu Feb 16, 2023 11:18 pm

Mmm... I wonder if this is an @autorelease pool issue. i.e. The ptrs getting released too early.

I tried commenting out every instance of @autoreleasepool and saw no difference in Tutorial00.

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5477
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1359

Re: flashing problem with samples on Metal in macOS Ventura

Post by dark_sylinc »

Great! After commenting them out, add one here:

Code: Select all

@autoreleasepool
{
    while( !bQuit )
    {
        WindowEventUtilities::messagePump();
        bQuit |= myWindowEventListener.getQuit();
        if( !bQuit )
            bQuit |= !root->renderOneFrame();
    }
}

You will have to rename the file Tutorial00_Basic.cpp to Tutorial00_Basic.mm though

jwwalker
Goblin
Posts: 268
Joined: Thu Aug 12, 2021 10:06 pm
Location: San Diego, CA, USA
x 19

Re: flashing problem with samples on Metal in macOS Ventura

Post by jwwalker »

Still no change with the new @autoreleasepool.

jwwalker
Goblin
Posts: 268
Joined: Thu Aug 12, 2021 10:06 pm
Location: San Diego, CA, USA
x 19

Re: flashing problem with samples on Metal in macOS Ventura

Post by jwwalker »

I think I've solved it. Consider this code from OgreMetalWindow.mm:

Code: Select all

    void MetalWindow::swapBuffers()
    {
        if( !mDevice->mFrameAborted )
        {
            // Schedule a present once rendering to the framebuffer is complete
            const CFTimeInterval presentationTime = mMetalView.presentationTime;

            if( mMetalLayer.presentsWithTransaction )
            {
                id<MTLCommandBuffer> commandBuffer = mDevice->mCurrentCommandBuffer;
                mDevice->commitAndNextCommandBuffer();
                [commandBuffer waitUntilScheduled];
                [mCurrentDrawable present];
            }
            else if( presentationTime < 0 )
            {
                [mDevice->mCurrentCommandBuffer presentDrawable:mCurrentDrawable];
            }
            else
            {
                [mDevice->mCurrentCommandBuffer presentDrawable:mCurrentDrawable
                                                         atTime:presentationTime];
            }
        }

        if( !mManualRelease )
            mCurrentDrawable = 0;
    }

Normally, presentsWithTransaction is false, and the view's presentationTime has not been set, so it has its initial value of 0. Therefore the last else clause runs. I'm guessing that what changed in Ventura is the behavior of -[MtlCommandBuffer presentDrawable:atTime:] when it is passed a time that is 0, or maybe anything less than the current time. If we change the last else if to

Code: Select all

else if( presentationTime <= 0.0 )

then the flicker goes away.

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5477
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1359

Re: flashing problem with samples on Metal in macOS Ventura

Post by dark_sylinc »

You're telling me that it flickers when presentationTime == 0 (and exactly 0)???

That's a massive bug on their end.

In the meantime we can workaround it.

Good detective work there! Thanks!

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5477
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1359

Re: flashing problem with samples on Metal in macOS Ventura

Post by dark_sylinc »

Oh I confused presentDrawable atTime with afterMinimumDuration.

Looking at documentation, atTime is a mach absolute value. Passing a value of 0 is definitely wrong.

Update: Passing a value of 0 should be right. What is wrong is the default value shouldn't be 0, but -1

Update 2: OgreMetalView.mm sets presentationTime to -1. Why is it not -1 in your case?
Update 3: TutorialViewController.mm is probably buggy as well.

jwwalker
Goblin
Posts: 268
Joined: Thu Aug 12, 2021 10:06 pm
Location: San Diego, CA, USA
x 19

Re: flashing problem with samples on Metal in macOS Ventura

Post by jwwalker »

dark_sylinc wrote: Sat Feb 18, 2023 1:40 am

Update 2: OgreMetalView.mm sets presentationTime to -1. Why is it not -1 in your case?

I don't see anything in OgreMetalView.mm that sets presentationTime to anything.

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5477
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1359

Re: flashing problem with samples on Metal in macOS Ventura

Post by dark_sylinc »

OH, you are right. It's only set in iOS, but not macOS.

This is a bug in our end.

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5477
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1359

Re: flashing problem with samples on Metal in macOS Ventura

Post by dark_sylinc »

Fixed.

I also double checked TutorialViewController.mm, that one is right after all, because it is relative to CADisplayLink::timestamp

Thanks for looking into this problem so thoroughly.

jwwalker
Goblin
Posts: 268
Joined: Thu Aug 12, 2021 10:06 pm
Location: San Diego, CA, USA
x 19

Re: flashing problem with samples on Metal in macOS Ventura

Post by jwwalker »

You're welcome, and thanks for your attention to it.