VSync WaitVBL implementation

A place to discuss current and future developments for STeem

Moderators: Mug UK, Steem Authors, Moderator Team

mlynn1974
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 119
Joined: Mon Mar 03, 2008 10:33 pm
Contact:

VSync WaitVBL implementation

Postby mlynn1974 » Mon Oct 26, 2015 2:06 am

Hi Everyone,

I have been trying to rewrite my DirectX PC demo code so it can run in a Window and FullScreen, just like Steem.
My code uses WaitForVerticalBlank() which hogs the CPU time. I didn't really care 10 years ago, but with laptops running so fast these days I think my little demos should only use about 3% CPU time.

The problem is waiting for a vertical blank in DirectX without using all the CPU time waiting for the vertical blank.

I thought I would check the source code and see how it does it, but I am a bit confused.
In display.cpp we have SteemDisplay::VSync()

It cleverly uses GetScanLine() to wait until it is at line 480 or 400 then calls WaitForVerticalBlank() to hog the CPU a bit. Also unexpecetdly the Sleep(0) code is commented out, so how does Windows yield to other threads? On a 2GHz CPU GetScanLine() might be called 140 times per scanline!

On a Centrino 2GHz dual core Steem uses 30-40% on one core. This is OK I guess, but in this day of many different screen resolutions and refresh rates shouldn't this be dynamic and use the screen resolution to determine the scan line?

Of course the problem is that Windows is so bad at timing and DirectX doesn't have a low overhead WaitForVerticalBlank() method. A sleep(0) might have a 20ms jitter so it could easily miss the vertical blank.

Leonard's code in his demos seems to do it correctly with low CPU use but I don't have the source code to these demos. Any advice would be very helpful.
Still got, still working: Atari 4Mb STe, 520STFM, 2.5Mb STF.
Hardware: Cumana CSA 354, Ultimate Ripper, Blitz Turbo, Synchro Express II (US and UK Versions).

mlynn1974
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 119
Joined: Mon Mar 03, 2008 10:33 pm
Contact:

Re: VSync WaitVBL implementation

Postby mlynn1974 » Sat Oct 31, 2015 1:18 am

After some digging I found that this code works:

Code: Select all

void DoEvents()
{
    MSG msg;
 
    while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
    {
        if ( GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
            break;
    }
}

void VSync()
{
   BOOL Blanking=FALSE;
   do
   {
      lpDD->GetVerticalBlankStatus(&Blanking);
      DoEvents();
      Sleep(1);
   } while(Blanking==FALSE && lpDD != NULL);
}


The Sleep(1) is preferrable to Sleep(0) because Sleep(0) will only yield to other threads of the same or higher priority. If you have a lower priority thread as a consumer it might not get a time slice. I have tested this on single core Celeron (Windows XP), Centrino (Dual Core) (Windows Vista) and an i5 (Windows 7) and it works well. I have not tested it on an old Pentium 4 HT or P3-800 or anything that old.

This reference was quite useful:
http://joeduffyblog.com/2006/08/22/prio ... t-manager/

I don't know if this would be a drop in replacement for SteemDisplay::VSync() in Steem but it might be helpful for someone.
Still got, still working: Atari 4Mb STe, 520STFM, 2.5Mb STF.
Hardware: Cumana CSA 354, Ultimate Ripper, Blitz Turbo, Synchro Express II (US and UK Versions).

User avatar
Steven Seagal
Atari God
Atari God
Posts: 1889
Joined: Sun Dec 04, 2005 9:12 am
Location: Undisclosed
Contact:

Re: VSync WaitVBL implementation

Postby Steven Seagal » Sat Oct 31, 2015 8:53 am

Hi,
You didn't post this in the development section so I don't know if you're aware of Steem "SSE", but when I tested it, Sleep(1) after GetVerticalBlankStatus() was a sure way to miss the VBLANK and so VSYNC didn't work at all.
Besides, CPU use during VSYNC isn't outrageous in Steem.
This could very well be system-dependent.

mlynn1974
Obsessive compulsive Atari behavior
Obsessive compulsive Atari behavior
Posts: 119
Joined: Mon Mar 03, 2008 10:33 pm
Contact:

Re: VSync WaitVBL implementation

Postby mlynn1974 » Sun Nov 01, 2015 1:59 am

Hi,

Sorry for not putting this in the developers forum, can it be moved there?
Yes I am aware of Steem SSE but my development machine runs Windows Vista and I haven't had the time to figure out why DirectX doesn't come with d3dx9_43.dll so I can't run it. I use Steem v3.2 and SteemDebug v3.2

>Besides, CPU use during VSYNC isn't outrageous in Steem.
True, I was trying to find a best practice example for this. I checked the GitHub source for v3.2 but I take it that is a base line and that Steem SSE is a lot newer.

>This could very well be system-dependent.
I think it could be. I will check out Steem SSE when I upgrade to Windows 7.

Regards,
Michael.
Still got, still working: Atari 4Mb STe, 520STFM, 2.5Mb STF.
Hardware: Cumana CSA 354, Ultimate Ripper, Blitz Turbo, Synchro Express II (US and UK Versions).


Social Media

     

Return to “Development”

Who is online

Users browsing this forum: No registered users and 1 guest