Skip to content

Add FNA3D_REDUCED_FRAME_LATENCY support for D3D11 #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/FNA3D_Driver_D3D11.c
Original file line number Diff line number Diff line change
Expand Up @@ -2537,8 +2537,10 @@ static void D3D11_INTERNAL_CreateSwapChain(
HWND dxgiHandle;
void* factory4;
IDXGISwapChain3 *swapchain3;
IDXGIDevice1 *dxgiDevice;
DXGI_COLOR_SPACE_TYPE colorSpace;
HRESULT res;
int reducedLatency = SDL_GetHintBoolean("FNA3D_REDUCED_FRAME_LATENCY", SDL_FALSE);

uint8_t growSwapchains = (swapchainData == NULL);

Expand All @@ -2564,7 +2566,7 @@ static void D3D11_INTERNAL_CreateSwapChain(
swapchainDesc.SampleDesc.Count = 1;
swapchainDesc.SampleDesc.Quality = 0;
swapchainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapchainDesc.BufferCount = 3;
swapchainDesc.BufferCount = reducedLatency ? 2 : 3;
swapchainDesc.OutputWindow = dxgiHandle;
swapchainDesc.Windowed = 1;
if (renderer->supportsTearing)
Expand Down Expand Up @@ -2668,6 +2670,30 @@ static void D3D11_INTERNAL_CreateSwapChain(
}
}

if (reducedLatency)
{
/*
If possible, limit queued frames to 1. (The default is 3.)
This significantly reduces rendering latency, especially in windowed mode.
Note that this will reduce maximum framerate somewhat.
*/
if (SUCCEEDED(IDXGIDevice1_QueryInterface(
renderer->device,
&D3D_IID_IDXGIDevice1,
(void**)&dxgiDevice
)))
{
if (!SUCCEEDED(IDXGIDevice1_SetMaximumFrameLatency(dxgiDevice, 1)))
{
FNA3D_LogWarn("IDXGIDevice1_SetMaximumFrameLatency failed");
}
}
else
{
FNA3D_LogWarn("IDXGIDevice1_QueryInterface failed");
}
}

if (growSwapchains)
{
swapchainData = (D3D11SwapchainData*) SDL_malloc(sizeof(D3D11SwapchainData));
Expand Down
1 change: 1 addition & 0 deletions src/FNA3D_Driver_D3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static const IID D3D_IID_IDXGIFactory4 = {0x7632e1f5,0xee65,0x4dca,{0x87,0xfd,0x
static const IID D3D_IID_IDXGIFactory6 = {0xc1b6694f,0xff09,0x44a9,{0xb0,0x3c,0x77,0x90,0x0a,0x0a,0x1d,0x17}};
static const IID D3D_IID_IDXGIFactory5 = {0x7632e1f5,0xee65,0x4dca,{0x87,0xfd,0x84,0xcd,0x75,0xf8,0x83,0x8d}};
static const IID D3D_IID_IDXGIAdapter1 = {0x29038f61,0x3839,0x4626,{0x91,0xfd,0x08,0x68,0x79,0x01,0x1a,0x05}};
static const IID D3D_IID_IDXGIDevice1 = {0x77db970f,0x6276,0x48ba,{0xba,0x28,0x07,0x01,0x43,0xb4,0x39,0x2c}};
static const IID D3D_IID_IDXGISwapChain3 = {0x94d99bdb,0xf1f8,0x4ab0,{0xb2,0x36,0x7d,0xa0,0x17,0x0e,0xda,0xb1}};
static const IID D3D_IID_ID3D11Texture2D = {0x6f15aaf2,0xd208,0x4e89,{0x9a,0xb4,0x48,0x95,0x35,0xd3,0x4f,0x9c}};
static const IID D3D_IID_ID3DUserDefinedAnnotation = {0xb2daad8b,0x03d4,0x4dbf,{0x95,0xeb,0x32,0xab,0x4b,0x63,0xd0,0xab}};
Expand Down