Skip to content

Commit

Permalink
Added vsync support and runtime toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
vilbeyli committed Jul 23, 2020
1 parent 2cea827 commit c0bf452
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Libs/VQUtils
Submodule VQUtils updated 1 files
+6 −0 Source/utils.h
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ VQE can be configured through `Data/EngineConfig.ini` file
| :-- | :-- |
| `ResolutionX=<int>` | Sets application render resolution width |
| `ResolutionY=<int>` | Sets application render resolution height |
| `VSync=<bool>` <br/> (TO BE IMPLEMENTED) | Toggles VSync based on the specified `<bool>` |
| `VSync=<bool>` <br/> | Toggles VSync based on the specified `<bool>` |

<br/>

Expand All @@ -75,7 +75,8 @@ VQE supports the following command line parameters:
| `-ResX=<int>` | Sets application render resolution width |
| `-ResY=<int>` | Sets application render resolution height |
| `-FullScreen` | Launches in fullscreen |
| `-VSync` | Enables VSync (TO BE IMPLEMENTED) |
| `-VSync` | Enables VSync |
| `-VSync=<bool>` | Sets Specified VSync State |
| `-TripleBuffering` | Initializes SwapChain with 3 back buffers |
| `-DoubleBuffering` | Initializes SwapChain with 2 back buffers |

Expand Down
12 changes: 1 addition & 11 deletions Source/Application/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ void Camera::InitializeCamera(const FCameraData& data)
this->mProjParams.FieldOfView = data.fovV_Degrees * DEG2RAD;
this->mProjParams.bPerspectiveProjection = data.bPerspectiveProjection;

mYaw = mPitch = 0;
SetProjectionMatrix(this->mProjParams);

SetPosition(data.x, data.y, data.z);
mYaw = mPitch = 0;
Rotate(data.yaw * DEG2RAD, data.pitch * DEG2RAD, 1.0f);
}

Expand Down Expand Up @@ -172,15 +171,6 @@ void Camera::Rotate(float yaw, float pitch, const float dt)
if (mPitch < -90.0f * DEG2RAD) mPitch = -90.0f * DEG2RAD;
}


//void Camera::Reset() // TODO: input
//{
// const Settings::Camera & data = m_settings;
// SetPosition(data.x, data.y, data.z);
// mYaw = mPitch = 0;
// Rotate(data.yaw * DEG2RAD, data.pitch * DEG2RAD, 1.0f);
//}

// internal update functions
void Camera::Rotate(const float dt, const FCameraInput& input)
{
Expand Down
7 changes: 6 additions & 1 deletion Source/Application/Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum EEventType
WINDOW_CLOSE_EVENT,
TOGGLE_FULLSCREEN_EVENT,
SET_FULLSCREEN_EVENT,
SET_VSYNC_EVENT,

// Windows->VQE input events
KEY_DOWN_EVENT,
Expand Down Expand Up @@ -119,7 +120,11 @@ struct SetFullscreenEvent : public IEvent
SetFullscreenEvent(HWND hwnd_, bool bFullscreen) : IEvent(EEventType::SET_FULLSCREEN_EVENT, hwnd_), bToggleValue(bFullscreen) {}
bool bToggleValue = false;
};

struct SetVSyncEvent : public IEvent
{
SetVSyncEvent(HWND hwnd_, bool bVSync) : IEvent(EEventType::SET_VSYNC_EVENT, hwnd_), bToggleValue(bVSync) {}
bool bToggleValue = false;
};

//
// INPUT EVENTS
Expand Down
2 changes: 1 addition & 1 deletion Source/Application/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static const Input::KeyMapping KEY_MAP = []()
m["C"] = 67; m["c"] = 67; m["N"] = 78; m["n"] = 78;
m["R"] = 82; m["r"] = 82; m["T"] = 'T'; m["t"] = 'T';
m["F"] = 'F'; m["f"] = 'F'; m["J"] = 'J'; m["j"] = 'J';
m["K"] = 'K'; m["k"] = 'K';
m["K"] = 'K'; m["k"] = 'K'; m["V"] = 'V';


m["\\"] = 220; m[";"] = 186;
Expand Down
11 changes: 9 additions & 2 deletions Source/Application/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ void ParseCommandLineParameters(FStartupParameters& refStartupParams, PSTR pScmd
}
if (paramName == "-VSync" || paramName == "-vsync" || paramName == "-Vsync")
{
refStartupParams.bOverrideGFXSetting_bVSync= true;
refStartupParams.EngineSettings.gfx.bVsync= true;
refStartupParams.bOverrideGFXSetting_bVSync = true;
if (paramValue.empty())
{
refStartupParams.EngineSettings.gfx.bVsync = true;
}
else
{
refStartupParams.EngineSettings.gfx.bVsync = StrUtil::ParseBool(paramValue);
}
}
if (paramName == "-TripleBuffering")
{
Expand Down
1 change: 1 addition & 0 deletions Source/Application/VQEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class VQEngine : public IWindowOwner
void RenderThread_HandleWindowResizeEvent(const std::shared_ptr<IEvent>& pEvent);
void RenderThread_HandleWindowCloseEvent(const IEvent* pEvent);
void RenderThread_HandleToggleFullscreenEvent(const IEvent* pEvent);
void RenderThread_HandleSetVSyncEvent(const IEvent* pEvent);

void UpdateThread_HandleWindowResizeEvent(const std::shared_ptr<IEvent>& pEvent);

Expand Down
34 changes: 34 additions & 0 deletions Source/Application/VQEngine_EventHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ void VQEngine::RenderThread_HandleEvents()
case EEventType::WINDOW_RESIZE_EVENT : pLastResizeEventLookup[pEvent->hwnd] = std::static_pointer_cast<WindowResizeEvent>(pEvent); break;
case EEventType::TOGGLE_FULLSCREEN_EVENT : RenderThread_HandleToggleFullscreenEvent(pEvent.get()); break;
case EEventType::WINDOW_CLOSE_EVENT : RenderThread_HandleWindowCloseEvent(pEvent.get()); break;
case EEventType::SET_VSYNC_EVENT : RenderThread_HandleSetVSyncEvent(pEvent.get()); break;
}
}

Expand Down Expand Up @@ -385,3 +386,36 @@ void VQEngine::RenderThread_HandleToggleFullscreenEvent(const IEvent* pEvent)

}

void VQEngine::RenderThread_HandleSetVSyncEvent(const IEvent* pEvent)
{
const SetVSyncEvent* pToggleFSEvent = static_cast<const SetVSyncEvent*>(pEvent);
HWND hwnd = pToggleFSEvent->hwnd;
const bool bVsyncState = pToggleFSEvent->bToggleValue;
SwapChain& Swapchain = mRenderer.GetWindowSwapChain(pToggleFSEvent->hwnd);
const FWindowSettings& WndSettings = GetWindowSettings(hwnd);
std::unique_ptr<Window>& pWnd = GetWindow(hwnd);
const bool bExclusiveFullscreenTransition = WndSettings.DisplayMode == EDisplayMode::EXCLUSIVE_FULLSCREEN;
const bool bFullscreenState = bExclusiveFullscreenTransition ? Swapchain.IsFullscreen() : pWnd->IsFullscreen();
const int WIDTH = bFullscreenState ? pWnd->GetFullscreenWidth() : pWnd->GetWidth();
const int HEIGHT = bFullscreenState ? pWnd->GetFullscreenHeight() : pWnd->GetHeight();

Swapchain.WaitForGPU(); // make sure GPU is finished
{
auto& ctx = mRenderer.GetWindowRenderContext(hwnd);
FWindowRepresentation wndRep(pWnd, bVsyncState, Swapchain.IsFullscreen());

FSwapChainCreateDesc desc;
desc.bVSync = bVsyncState;
desc.bFullscreen = Swapchain.IsFullscreen();
desc.numBackBuffers = Swapchain.GetNumBackBuffers();
desc.pCmdQueue = &ctx.PresentQueue;
desc.pDevice = ctx.pDevice->GetDevicePtr();
desc.pWindow = &wndRep;

Swapchain.Destroy();
Swapchain.Create(desc);
}

Log::Info("Toggle VSync: %d", bVsyncState);
}

93 changes: 46 additions & 47 deletions Source/Application/VQEngine_Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "Libs/VQUtils/Source/utils.h"

#include <fstream>
#include <sstream>
#include <cassert>

#ifdef _DEBUG
Expand All @@ -32,6 +31,42 @@ constexpr char* BUILD_CONFIG = "Release";
constexpr char* VQENGINE_VERSION = "v0.3.0";


static std::pair<std::string, std::string> ParseLineINI(const std::string& iniLine)
{
assert(!iniLine.empty());
std::pair<std::string, std::string> SettingNameValuePair;

const bool bSectionTag = iniLine[0] == '[';

if (bSectionTag)
{
auto vecSettingNameValue = StrUtil::split(iniLine.substr(1), ']');
SettingNameValuePair.first = vecSettingNameValue[0];
}
else
{
auto vecSettingNameValue = StrUtil::split(iniLine, '=');
assert(vecSettingNameValue.size() >= 2);
SettingNameValuePair.first = vecSettingNameValue[0];
SettingNameValuePair.second = vecSettingNameValue[1];
}


return SettingNameValuePair;
}

static std::unordered_map<std::string, EDisplayMode> S_LOOKUP_STR_TO_DISPLAYMODE =
{
{ "Fullscreen" , EDisplayMode::EXCLUSIVE_FULLSCREEN }
, { "Borderless" , EDisplayMode::BORDERLESS_FULLSCREEN }
, { "BorderlessFullscreen" , EDisplayMode::BORDERLESS_FULLSCREEN }
, { "BorderlessWindowed" , EDisplayMode::BORDERLESS_FULLSCREEN }
, { "Windowed" , EDisplayMode::WINDOWED }
};




void VQEngine::MainThread_Tick()
{
if (this->mSettings.bAutomatedTestRun)
Expand Down Expand Up @@ -276,42 +311,6 @@ void VQEngine::UnregisterWindowForInput(const std::unique_ptr<Window>& pWnd)



static std::pair<std::string, std::string> ParseLineINI(const std::string& iniLine)
{
assert(!iniLine.empty());
std::pair<std::string, std::string> SettingNameValuePair;

const bool bSectionTag = iniLine[0] == '[';

if (bSectionTag)
{
auto vecSettingNameValue = StrUtil::split(iniLine.substr(1), ']');
SettingNameValuePair.first = vecSettingNameValue[0];
}
else
{
auto vecSettingNameValue = StrUtil::split(iniLine, '=');
assert(vecSettingNameValue.size() >= 2);
SettingNameValuePair.first = vecSettingNameValue[0];
SettingNameValuePair.second = vecSettingNameValue[1];
}


return SettingNameValuePair;
}
static bool ParseBool(const std::string& s) { bool b; std::istringstream(s) >> std::boolalpha >> b; return b; }
static int ParseInt(const std::string& s) { return std::atoi(s.c_str()); }
static float ParseFloat(const std::string& s) { return static_cast<float>(std::atof(s.c_str())); }

static std::unordered_map<std::string, EDisplayMode> S_LOOKUP_STR_TO_DISPLAYMODE =
{
{ "Fullscreen" , EDisplayMode::EXCLUSIVE_FULLSCREEN }
, { "Borderless" , EDisplayMode::BORDERLESS_FULLSCREEN }
, { "BorderlessFullscreen" , EDisplayMode::BORDERLESS_FULLSCREEN }
, { "BorderlessWindowed" , EDisplayMode::BORDERLESS_FULLSCREEN }
, { "Windowed" , EDisplayMode::WINDOWED }
};

FStartupParameters VQEngine::ParseEngineSettingsFile()
{
constexpr char* ENGINE_SETTINGS_FILE_NAME = "Data/EngineSettings.ini";
Expand Down Expand Up @@ -345,17 +344,17 @@ FStartupParameters VQEngine::ParseEngineSettingsFile()
if (SettingName == "VSync")
{
params.bOverrideGFXSetting_bVSync = true;
params.EngineSettings.gfx.bVsync = ParseBool(SettingValue);
params.EngineSettings.gfx.bVsync = StrUtil::ParseBool(SettingValue);
}
if (SettingName == "RenderScale")
{
params.bOverrideGFXSetting_RenderScale = true;
params.EngineSettings.gfx.RenderScale = ParseFloat(SettingValue);
params.EngineSettings.gfx.RenderScale = StrUtil::ParseFloat(SettingValue);
}
if (SettingName == "TripleBuffer")
{
params.bOverrideGFXSetting_bUseTripleBuffering = true;
params.EngineSettings.gfx.bUseTripleBuffering = ParseBool(SettingValue);
params.EngineSettings.gfx.bUseTripleBuffering = StrUtil::ParseBool(SettingValue);
}


Expand All @@ -365,12 +364,12 @@ FStartupParameters VQEngine::ParseEngineSettingsFile()
if (SettingName == "Width")
{
params.bOverrideENGSetting_MainWindowWidth = true;
params.EngineSettings.WndMain.Width = ParseInt(SettingValue);
params.EngineSettings.WndMain.Width = StrUtil::ParseInt(SettingValue);
}
if (SettingName == "Height")
{
params.bOverrideENGSetting_MainWindowHeight = true;
params.EngineSettings.WndMain.Height = ParseInt(SettingValue);
params.EngineSettings.WndMain.Height = StrUtil::ParseInt(SettingValue);
}
if (SettingName == "DisplayMode")
{
Expand All @@ -383,24 +382,24 @@ FStartupParameters VQEngine::ParseEngineSettingsFile()
if (SettingName == "PreferredDisplay")
{
params.bOverrideENGSetting_PreferredDisplay = true;
params.EngineSettings.WndMain.PreferredDisplay = ParseInt(SettingValue);
params.EngineSettings.WndMain.PreferredDisplay = StrUtil::ParseInt(SettingValue);
}


if (SettingName == "DebugWindow")
{
params.bOverrideENGSetting_bDebugWindowEnable = true;
params.EngineSettings.bShowDebugWindow = ParseBool(SettingValue);
params.EngineSettings.bShowDebugWindow = StrUtil::ParseBool(SettingValue);
}
if (SettingName == "DebugWindowWidth")
{
params.bOverrideENGSetting_DebugWindowWidth = true;
params.EngineSettings.WndDebug.Width = ParseInt(SettingValue);
params.EngineSettings.WndDebug.Width = StrUtil::ParseInt(SettingValue);
}
if (SettingName == "DebugWindowHeight")
{
params.bOverrideENGSetting_DebugWindowHeight = true;
params.EngineSettings.WndDebug.Height = ParseInt(SettingValue);
params.EngineSettings.WndDebug.Height = StrUtil::ParseInt(SettingValue);
}
if (SettingName == "DebugWindowDisplayMode")
{
Expand All @@ -413,7 +412,7 @@ FStartupParameters VQEngine::ParseEngineSettingsFile()
if (SettingName == "DebugWindowPreferredDisplay")
{
params.bOverrideENGSetting_DebugWindowPreferredDisplay = true;
params.EngineSettings.WndDebug.PreferredDisplay = ParseInt(SettingValue);
params.EngineSettings.WndDebug.PreferredDisplay = StrUtil::ParseInt(SettingValue);
}

}
Expand Down
6 changes: 3 additions & 3 deletions Source/Application/VQEngine_Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void VQEngine::RenderThread_RenderDebugWindow()
//
// PRESENT
//
hr = ctx.SwapChain.Present(ctx.bVsync);
hr = ctx.SwapChain.Present();
ctx.SwapChain.MoveToNextFrame();
//return hr;
}
Expand Down Expand Up @@ -446,7 +446,7 @@ HRESULT VQEngine::RenderThread_RenderMainWindow_LoadingScreen(FWindowRenderConte
//
// PRESENT
//
hr = ctx.SwapChain.Present(ctx.bVsync);
hr = ctx.SwapChain.Present();
ctx.SwapChain.MoveToNextFrame();
return hr;
}
Expand Down Expand Up @@ -583,7 +583,7 @@ HRESULT VQEngine::RenderThread_RenderMainWindow_Scene(FWindowRenderContext& ctx)
//
// PRESENT
//
hr = ctx.SwapChain.Present(ctx.bVsync);
hr = ctx.SwapChain.Present();
ctx.SwapChain.MoveToNextFrame();
return hr;
}
Expand Down
10 changes: 9 additions & 1 deletion Source/Application/VQEngine_Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void VQEngine::HandleEngineInput()
Input& input = it->second;
auto& pWin = this->GetWindow(hwnd);

if (input.IsKeyDown("Esc"))
if (input.IsKeyTriggered("Esc"))
{
if (pWin->IsMouseCaptured())
{
Expand All @@ -159,6 +159,14 @@ void VQEngine::HandleEngineInput()
mEventQueue_VQEToWin_Main.AddItem(std::make_shared<SetMouseCaptureEvent>(hwnd, true, false));
}
}
if (input.IsKeyTriggered("V"))
{
if (pWin == mpWinMain)
{
auto& SwapChain = mRenderer.GetWindowSwapChain(hwnd);
mEventQueue_WinToVQE_Renderer.AddItem(std::make_shared<SetVSyncEvent>(hwnd, !SwapChain.IsVSyncOn()));
}
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions Source/Renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,14 @@ void VQRenderer::InitializeRenderContext(const FWindowRepresentation& WndDesc, i

ctx.pDevice = pVQDevice;
ctx.PresentQueue.Create(ctx.pDevice, CommandQueue::ECommandQueueType::GFX); // Create the GFX queue for presenting the SwapChain
ctx.bVsync = WndDesc.bVSync; // we store bVSync in ctx instead of SwapChain and provide it through SwapChain.Present() param : either way should be fine

// Create the SwapChain
FSwapChainCreateDesc swapChainDesc = {};
swapChainDesc.numBackBuffers = NumSwapchainBuffers;
swapChainDesc.pDevice = ctx.pDevice->GetDevicePtr();
swapChainDesc.pWindow = &WndDesc;
swapChainDesc.pCmdQueue = &ctx.PresentQueue;
swapChainDesc.bVSync = ctx.bVsync;
swapChainDesc.bVSync = WndDesc.bVSync;
swapChainDesc.bFullscreen = WndDesc.bExclusiveFullscreen;
ctx.SwapChain.Create(swapChainDesc);

Expand Down
2 changes: 0 additions & 2 deletions Source/Renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ struct FWindowRenderContext

ID3D12GraphicsCommandList* pCmdList_GFX = nullptr;

bool bVsync = false;

int MainRTResolutionX = -1;
int MainRTResolutionY = -1;
};
Expand Down
Loading

0 comments on commit c0bf452

Please sign in to comment.