Jump to content
  • Advertisement
Sign in to follow this  
cozzie

d3d11 and vsync

This topic is 653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I'm trying to implement the correct enabling/ disabling of vsync in my (new) d3d11 engine.

From what I've read/ understand, it's quite easy:

bool MainApp::Draw()
{
	float color[4] = { 0.3f, 0.3f, 0.0f, 1.0f };
	mD3d11.GetContextPtr()->ClearRenderTargetView(mD3d11.GetRTVPtr(), color);
	mD3d11.GetContextPtr()->ClearDepthStencilView(mD3d11.GetDepthStencilViewPtr(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	/** DRAW STUFF **/

	if(mD3d11.mSettings.GetVsync())
	{
		if(FAILED(mD3d11.GetSwapChainPtr()->Present(1, 0))) return false;
	}
	else if(FAILED(mD3d11.GetSwapChainPtr()->Present(0, 0))) return false;

	return true;
}

My questions;

- is this the right way to go?

- so far, I Always return false for GetVsync if I'm in windowed mode, because I believe vsync only 'works' fullscreen.

But when I enable it in windowed mode, the frame time also changes/ something 'is syncing. How can this be explained?

(btw; the frame time then becomes 8.33 ms / 120FPS, because my refresh rate currently is 120hz).

-- my conclusion is that in windowed mode with present(1..)/ vsync enabled, the timing is matched to the current desktop's refreshrate. And in fullscreen mode it's matched to the refreshrate set in the swapchain desc:

 

BufferDesc.RefreshRate.Numerator = 60;

BufferDesc.RefreshRate.Denominator = 1;

 

I'm wondering if I should support vsync on/off in windowed mode, or just always of/ disabled.

 

Any input is appreciated is always.

Edited by cozzie

Share this post


Link to post
Share on other sites
Advertisement

- Regarding whether it's right or wrong, ask yourself what do you want to achieve?

In games, leave vsync as an tweakable menu option. However, this means that you'll have to make sure your code ticks at your desired frame rate in spite of the setting so as to achieve consistency.

- when you call present(1, 0) on a thread, the thread blocks until the syncing of the refresh rate. Hence the running of the next frame of code has to wait as well.

Share this post


Link to post
Share on other sites

Thanks.

Regarding point 1; clear, I should provide the user the possibility to enable/disable vsync, without affecting gameplay/ logics/ the main loop.

 

On the present(1,0), what do you exactly mean with 'running of the next frame of code has to wait as well', I thought this simply 'enabled' vsync (which also seems to be the result, frame times constant 16.67ms, aka 1 / 60 sec). I know I can also pass 2 tot 4 instead of 0 or 1, but I'm not sure how that affects the result.

Share this post


Link to post
Share on other sites

Ever heard of a blocking function? It's the same concept. It waits until some condition has been fulfilled. present(1,0) waits until the refresh rate is hit, which is what vsync does.

 

For information on present and it's parameters, i recommend you check msdn.

Share this post


Link to post
Share on other sites

VSYNC is still relevent for windowed mode. If you use a sync interval of 1, then the GPU will wait until the display's vertical refresh to present the back buffer. If you pass 0, then it will present as quickly as possible. The one difference vs exclusive fullscreen is that the GPU is presenting the backbuffer to the desktop composition engine (DWM) instead of directly to the screen. As a result if you use a sync interval of 1, you'll see your game run at an uncapped framerate but it won't have any tearing. This is because the composition engine is still presenting with VSYNC enabled, at a rate that's decoupled from when your app presents. In exclusive fullscreen, disabling VSYNC will result in horizontal tearing.

 

EDIT: I should add that things changed a bit with Win 10 and the addition of the FLIP swap modes, which don't work exactly the same way as I described above See the docs for DXGI_SWAP_EFFECT for more details

Edited by MJP

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!