Skip to content

Commit

Permalink
RendererAPI/VulkanRenderer Added support for AMD Anti Lag 2 (VK_AMD_a…
Browse files Browse the repository at this point in the history
…nti_lag) 11/05/2024 | 24w45a2
  • Loading branch information
GamesTrap committed Nov 5, 2024
1 parent 8f70d76 commit 5cf10c0
Show file tree
Hide file tree
Showing 17 changed files with 490 additions and 31 deletions.
35 changes: 33 additions & 2 deletions Games/TRAP-Editor/src/TRAPEditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ namespace

//-------------------------------------------------------------------------------------------------------------------//

void DrawLatencyModeSetting()
void DrawNVIDIAReflexSetting()
{
static constexpr std::array<TRAP::Graphics::NVIDIAReflexLatencyMode, 3u> latencyModes
{
Expand Down Expand Up @@ -231,6 +231,36 @@ namespace

//-------------------------------------------------------------------------------------------------------------------//

void DrawAMDAntiLagSetting()
{
static constexpr std::array<TRAP::Graphics::AMDAntiLagMode, 3u> modes
{
TRAP::Graphics::AMDAntiLagMode::DriverControl,
TRAP::Graphics::AMDAntiLagMode::Disabled,
TRAP::Graphics::AMDAntiLagMode::Enabled
};
const TRAP::Graphics::AMDAntiLagMode currentMode = TRAP::Graphics::RenderCommand::GetAntiLagMode();

ImGui::BeginDisabled(!TRAP::Graphics::RendererAPI::GPUSettings.AntiLagSupported);
if(ImGui::BeginCombo("AMD Anti Lag", fmt::format("{}", currentMode).c_str()))
{
for(const auto mode : modes)
{
const bool isSelected = mode == currentMode;
if(ImGui::Selectable(fmt::format("{}", mode).c_str(), isSelected))
TRAP::Graphics::RenderCommand::SetAntiLagMode(mode);

if(isSelected)
ImGui::SetItemDefaultFocus();
}

ImGui::EndCombo();
}
ImGui::EndDisabled();
}

//-------------------------------------------------------------------------------------------------------------------//

void DrawRenderScaleSetting()
{
f32 renderScale = TRAP::Graphics::RenderCommand::GetRenderScale();
Expand All @@ -252,7 +282,8 @@ namespace
DrawAntiAliasingSetting();
DrawAnisotropySetting();
DrawRenderScaleSetting();
DrawLatencyModeSetting();
DrawNVIDIAReflexSetting();
DrawAMDAntiLagSetting();
DrawRawMouseInputSetting(*window);
}
}
Expand Down
38 changes: 36 additions & 2 deletions Games/Tests/src/InputLag/InputLagTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace
{
void DrawLatencyModeSelection()
void DrawNVIDIAReflexModeSelection()
{
ImGui::Text("NVIDIA Reflex:");
if(!TRAP::Graphics::RendererAPI::GPUSettings.ReflexSupported)
Expand Down Expand Up @@ -35,6 +35,39 @@ namespace
}
}
}
void DrawAMDAntiLagModeSelection()
{
ImGui::Text("AMD Anti Lag:");
if(!TRAP::Graphics::RendererAPI::GPUSettings.AntiLagSupported)
ImGui::Text("AMD Anti Lag is not supported on this GPU!");
else
{
static constexpr std::array<TRAP::Graphics::AMDAntiLagMode, 3u> modes
{
TRAP::Graphics::AMDAntiLagMode::DriverControl,
TRAP::Graphics::AMDAntiLagMode::Disabled,
TRAP::Graphics::AMDAntiLagMode::Enabled
};
const TRAP::Graphics::AMDAntiLagMode currentMode = TRAP::Graphics::RenderCommand::GetAntiLagMode();

ImGui::Text("Current Mode: %s", fmt::format("{}", currentMode).c_str());

if(ImGui::BeginCombo("Mode", fmt::format("{}", currentMode).c_str()))
{
for(const auto mode : modes)
{
const bool isSelected = mode == currentMode;
if(ImGui::Selectable(fmt::format("{}", mode).c_str(), isSelected))
TRAP::Graphics::RenderCommand::SetAntiLagMode(mode);

if(isSelected)
ImGui::SetItemDefaultFocus();
}

ImGui::EndCombo();
}
}
}

void DrawNVIDIAReflexStats([[maybe_unused]] const InputLagTests::ReflexData& reflexData)
{
Expand Down Expand Up @@ -114,7 +147,8 @@ void InputLagTests::OnImGuiRender()
ImGui::Text("GPU Graphics FrameTime: %.3fms", TRAP::Graphics::RenderCommand::GetGPUGraphicsFrameTime());
ImGui::Text("GPU Compute FrameTime: %.3fms", TRAP::Graphics::RenderCommand::GetGPUComputeFrameTime());
ImGui::Separator();
DrawLatencyModeSelection();
DrawNVIDIAReflexModeSelection();
DrawAMDAntiLagModeSelection();
ImGui::End();

DrawNVIDIAReflexStats(m_reflexData);
Expand Down
1 change: 1 addition & 0 deletions SITREPS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5364,3 +5364,4 @@ SITREP 11/04/2024|24w45a1
SITREP 11/05/2024|24w45a2
- Changed TRAP Engine version to 24w45a2(0.11.37)
- Cleaned up Vulkan VK_NV_low_latency2 code ~<10 mins
- RendererAPI/VulkanRenderer Added support for AMD Anti Lag 2 (VK_AMD_anti_lag) ~>60 mins
25 changes: 20 additions & 5 deletions TRAP/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,19 @@ namespace
return;

#ifndef TRAP_HEADLESS_MODE
//FPS limiting will be handled by NVIDIA Reflex / AMD Anti Lag respectively when supported.
if(TRAP::Graphics::RendererAPI::GPUSettings.ReflexSupported)
{
TRAP::Graphics::RendererAPI::GetRenderer()->ReflexSleep(*TRAP::Application::GetWindow());
return;
}

//AMD Anti Lag only limits FPS when set to enabled.
if (TRAP::Graphics::RendererAPI::GPUSettings.AntiLagSupported &&
TRAP::Graphics::RendererAPI::GetRenderer()->GetAntiLagMode(*TRAP::Application::GetWindow()) != TRAP::Graphics::RendererAPI::AMDAntiLagMode::Disabled)
{
return;
}
#endif /*TRAP_HEADLESS_MODE*/

const std::chrono::duration<f32, std::milli> limitMs =
Expand Down Expand Up @@ -263,6 +271,8 @@ namespace
#ifndef TRAP_HEADLESS_MODE
//NVIDIA Reflex
config.Set("NVIDIAReflex", TRAP::Graphics::RenderCommand::GetReflexLatencyMode());
//AMD Anti Lag
config.Set("AMDAntiLag", TRAP::Graphics::RenderCommand::GetAntiLagMode());
#endif /*TRAP_HEADLESS_MODE*/
}
}
Expand Down Expand Up @@ -463,9 +473,11 @@ namespace
if(TRAP::Graphics::RendererAPI::GetRenderAPI() == TRAP::Graphics::RenderAPI::NONE)
return;

#if !defined(TRAP_HEADLESS_MODE)
#ifndef TRAP_HEADLESS_MODE
//NVIDIA Reflex
TRAP::Graphics::RenderCommand::SetReflexLatencyMode(config.Get<TRAP::Graphics::NVIDIAReflexLatencyMode>("NVIDIAReflex").value_or(TRAP::Graphics::NVIDIAReflexLatencyMode::Disabled));
//AMD Anti Lag
TRAP::Graphics::RenderCommand::SetAntiLagMode(config.Get<TRAP::Graphics::AMDAntiLagMode>("AMDAntiLag").value_or(TRAP::Graphics::AMDAntiLagMode::Disabled));
#endif /*!TRAP_HEADLESS_MODE*/

const f32 renderScale = config.Get<f32>("RenderScale").value_or(1.0f);
Expand Down Expand Up @@ -664,12 +676,13 @@ void TRAP::Application::Run()
#endif

#if !defined(TRAP_HEADLESS_MODE)
Graphics::RendererAPI::GetRenderer()->AntiLagMarker(TRAP::Graphics::RendererAPI::AMDAntiLagMarker::InputStage, *m_window);
Graphics::RendererAPI::GetRenderer()->ReflexMarker(TRAP::Graphics::RendererAPI::NVIDIAReflexLatencyMarker::SimulationStart, *m_window);
#endif /*!TRAP_HEADLESS_MODE*/

#ifndef TRAP_HEADLESS_MODE
TRAP::Window::OnUpdate();
Graphics::RendererAPI::GetRenderer()->ReflexMarker(TRAP::Graphics::RendererAPI::NVIDIAReflexLatencyMarker::InputSample, *m_window);
TRAP::Window::OnUpdate();
#endif /*TRAP_HEADLESS_MODE*/

#if !defined(TRAP_HEADLESS_MODE)
Expand Down Expand Up @@ -842,10 +855,12 @@ void TRAP::Application::SetFPSLimit(const u32 targetFPS)
s_Instance->m_fpsLimit = (targetFPS != 0u) ? TRAP::Math::Clamp(targetFPS, MinLimitedFPS, MaxLimitedFPS) : 0u;

#if !defined(TRAP_HEADLESS_MODE)
if(TRAP::Graphics::RendererAPI::GetRenderAPI() != TRAP::Graphics::RenderAPI::NONE &&
TRAP::Graphics::RendererAPI::GPUSettings.ReflexSupported)
if(TRAP::Graphics::RendererAPI::GetRenderAPI() != TRAP::Graphics::RenderAPI::NONE)
{
Graphics::RendererAPI::GetRenderer()->SetReflexFPSLimit(s_Instance->m_fpsLimit);
if(TRAP::Graphics::RendererAPI::GPUSettings.ReflexSupported)
Graphics::RendererAPI::GetRenderer()->SetReflexFPSLimit(s_Instance->m_fpsLimit);
else if(TRAP::Graphics::RendererAPI::GPUSettings.AntiLagSupported)
Graphics::RendererAPI::GetRenderer()->SetAntiLagFPSLimit(s_Instance->m_fpsLimit);
}
#endif /*!TRAP_HEADLESS_MODE*/
}
Expand Down
12 changes: 9 additions & 3 deletions TRAP/src/Graphics/API/Objects/SwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,26 @@ namespace TRAP::Graphics
/// @return Latency reports.
/// @remark @headless This function is not available in headless mode.
[[nodiscard]] virtual std::vector<VkLatencyTimingsFrameReportNV> ReflexGetLatency(u32 numLatencyData) const = 0;

/// @brief Set a timestamp for NVIDIA-Reflex.
/// @param marker Enum value of the marker to set.
virtual void ReflexSetMarker(TRAP::Graphics::RendererAPI::NVIDIAReflexLatencyMarker marker) = 0;

/// @brief Set the latency mode for NVIDIA-Reflex.
/// @param latencyMode Latency mode to use.
/// @param fpsLimit Optional: FPS limit, use 0 to disable limiter.
virtual void ReflexSetLatencyMode(TRAP::Graphics::RendererAPI::NVIDIAReflexLatencyMode latencyMode, u32 fpsLimit = 0u) const = 0;

/// @brief Delay host CPU work for low latency rendering.
/// @param reflexSemaphore Semaphore to use for sleep.
virtual void ReflexSleep(const Semaphore& reflexSemaphore) const = 0;

/// @brief Set a timestamp for AMD Anti Lag.
/// @param marker Enum value of the marker to set.
/// @param viewportData The viewport to set the marker for.
virtual void AntiLagSetMarker(TRAP::Graphics::RendererAPI::AMDAntiLagMarker marker, const RendererAPI::PerViewportData& viewportData) = 0;
/// @brief Set the mode for AMD Anti Lag.
/// @param mode Mode to use.
/// @param fpsLimit Optional: FPS limit, use 0 to disable limiter.
virtual void AntiLagSetMode(TRAP::Graphics::RendererAPI::AMDAntiLagMode mode, u32 fpsLimit = 0u) const = 0;

protected:
/// @brief Constructor.
SwapChain();
Expand Down
Loading

0 comments on commit 5cf10c0

Please sign in to comment.