Skip to content

Commit

Permalink
Fix "D3DSWAPEFFECT_COPY_VSYNC" not waiting for vblank in windowed mode
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire committed Sep 5, 2020
1 parent b26452e commit 15b82e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
56 changes: 32 additions & 24 deletions source/d3d8types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
#include "d3d8types.hpp"
#include <assert.h>

bool SupportsPalettes()
{
HDC hDC = GetDC(nullptr);
bool hasPalette = (GetDeviceCaps(hDC, RASTERCAPS) & RC_PALETTE) != 0;
ReleaseDC(nullptr, hDC);
return hasPalette;
}

static UINT CalcTextureSize(UINT Width, UINT Height, UINT Depth, D3DFORMAT Format)
{
switch (static_cast<DWORD>(Format))
Expand Down Expand Up @@ -64,6 +72,7 @@ static UINT CalcTextureSize(UINT Width, UINT Height, UINT Depth, D3DFORMAT Forma
return ((Width + 3) >> 2) * ((Height + 3) >> 2) * 16;
}
}

void ConvertCaps(D3DCAPS9 &Input, D3DCAPS8 &Output)
{
CopyMemory(&Output, &Input, sizeof(Output));
Expand All @@ -81,6 +90,7 @@ void ConvertCaps(D3DCAPS9 &Input, D3DCAPS8 &Output)
// D3D8 can only handle up to 256 for MaxVertexShaderConst
Output.MaxVertexShaderConst = min(256, Input.MaxVertexShaderConst);
}

void ConvertVolumeDesc(D3DVOLUME_DESC &Input, D3DVOLUME_DESC8 &Output)
{
Output.Format = Input.Format;
Expand All @@ -103,6 +113,7 @@ void ConvertSurfaceDesc(D3DSURFACE_DESC &Input, D3DSURFACE_DESC8 &Output)
Output.Width = Input.Width;
Output.Height = Input.Height;
}

void ConvertPresentParameters(D3DPRESENT_PARAMETERS8 &Input, D3DPRESENT_PARAMETERS &Output)
{
Output.BackBufferWidth = Input.BackBufferWidth;
Expand All @@ -117,43 +128,47 @@ void ConvertPresentParameters(D3DPRESENT_PARAMETERS8 &Input, D3DPRESENT_PARAMETE
Output.EnableAutoDepthStencil = Input.EnableAutoDepthStencil;
Output.AutoDepthStencilFormat = Input.AutoDepthStencilFormat;
Output.Flags = Input.Flags;
Output.FullScreen_RefreshRateInHz = Input.FullScreen_RefreshRateInHz;
Output.PresentationInterval = Input.FullScreen_PresentationInterval;

// MultiSampleType must be D3DMULTISAMPLE_NONE unless SwapEffect has been set to D3DSWAPEFFECT_DISCARD or if there is a lockable backbuffer
if (Output.SwapEffect != D3DSWAPEFFECT_DISCARD || (Output.Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER))
{
Output.MultiSampleType = D3DMULTISAMPLE_NONE;
}

// D3DPRESENT_RATE_UNLIMITED is no longer supported in D3D9
if (Output.FullScreen_RefreshRateInHz == D3DPRESENT_RATE_UNLIMITED)
{
Output.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
}

// Windowed mode should always present immediately
if (Output.Windowed)
if (Input.Windowed)
{
Output.FullScreen_RefreshRateInHz = 0;
// D3D8 always presents without waiting for vblank when windowed
Output.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
}

// Fulscreen mode defaults to D3DPRESENT_INTERVAL_ONE
if (!Output.Windowed && Output.PresentationInterval == D3DPRESENT_INTERVAL_DEFAULT)
else
{
Output.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
// D3DPRESENT_RATE_UNLIMITED is no longer supported in D3D9
if (Input.FullScreen_RefreshRateInHz == D3DPRESENT_RATE_UNLIMITED)
{
Output.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
}
else
{
Output.FullScreen_RefreshRateInHz = Input.FullScreen_RefreshRateInHz;
}

// D3DPRESENT_INTERVAL_DEFAULT is equivalent to D3DPRESENT_INTERVAL_ONE in D3D9
Output.PresentationInterval = Input.FullScreen_PresentationInterval;
}

// D3DSWAPEFFECT_COPY_VSYNC is no longer supported in D3D9
if (Output.SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
{
Output.SwapEffect = D3DSWAPEFFECT_COPY;
if (!Output.Windowed && Output.PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE)
Output.SwapEffect = D3DSWAPEFFECT_COPY;
// Need to wait for vblank before copying (both when windowed and full-screen)
if (Output.PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE)
{
Output.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
Output.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
}
}
}

void ConvertAdapterIdentifier(D3DADAPTER_IDENTIFIER9 &Input, D3DADAPTER_IDENTIFIER8 &Output)
{
CopyMemory(Output.Driver, Input.Driver, MAX_DEVICE_IDENTIFIER_STRING);
Expand All @@ -166,10 +181,3 @@ void ConvertAdapterIdentifier(D3DADAPTER_IDENTIFIER9 &Input, D3DADAPTER_IDENTIFI
Output.DeviceIdentifier = Input.DeviceIdentifier;
Output.WHQLLevel = Input.WHQLLevel;
}
bool SupportsPalettes()
{
HDC hDC = GetDC(nullptr);
bool hasPalette = (GetDeviceCaps(hDC, RASTERCAPS) & RC_PALETTE) != 0;
ReleaseDC(nullptr, hDC);
return hasPalette;
}
3 changes: 2 additions & 1 deletion source/d3d8types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ struct D3DADAPTER_IDENTIFIER8
DWORD WHQLLevel;
};

bool SupportsPalettes();

void ConvertCaps(D3DCAPS9 &input, D3DCAPS8 &output);
void ConvertVolumeDesc(D3DVOLUME_DESC &input, D3DVOLUME_DESC8 &output);
void ConvertSurfaceDesc(D3DSURFACE_DESC &input, D3DSURFACE_DESC8 &output);
void ConvertPresentParameters(D3DPRESENT_PARAMETERS8 &input, D3DPRESENT_PARAMETERS &output);
void ConvertAdapterIdentifier(D3DADAPTER_IDENTIFIER9 &input, D3DADAPTER_IDENTIFIER8 &output);
bool SupportsPalettes();

0 comments on commit 15b82e7

Please sign in to comment.