PerformanceFPS & VRAM Optimization
Docs/Performance/FPS & VRAM Optimization

FPS & VRAM Optimization

๐ŸŽฏ Project Prompt: Accurate FPS Counter with Python HUD (Windows 11, DirectX, CUDA/TensorRT) Objective:
Build a hybrid FPS counter system for a gaming application with a Python backend. Ensure accurate frame timing via DirectX (C++), expose results to Python via pybind11, and integrate CUDA/TensorRT timing separately. Package the solution as a DLL, compiled with MSVC, and distribute with the Microsoft Visual C++ Redistributable.

  1. Core FPS Measurement (C++ / DirectX) Hook into swapchain->Present() in DirectX 11/12.

Use QueryPerformanceCounter for highโ€‘precision timestamps.

Maintain a rolling buffer of frame times (e.g., 128 frames).

Compute FPS as 1 / average_frame_time.

cpp // fps_counter.cpp #include <windows.h>

static double frameTimes[128]; static int frameIndex = 0; static double lastTime = 0.0; static double fps = 0.0;

double GetTimeSeconds() { LARGE_INTEGER freq, counter; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&counter); return (double)counter.QuadPart / (double)freq.QuadPart; }

extern "C" __declspec(dllexport) void UpdateFPSCounter() { double currentTime = GetTimeSeconds(); double delta = currentTime - lastTime; lastTime = currentTime;

frameTimes[frameIndex % 128] = delta;
frameIndex++;

int count = min(frameIndex, 128);
double sum = 0.0;
for (int i = 0; i < count; i++) sum += frameTimes[i];

fps = 1.0 / (sum / count);

}

extern "C" __declspec(dllexport) double GetFPS() { return fps; } 2. Python Binding (pybind11) Create a binding module with pybind11:

cpp // pybind_fps.cpp #include <pybind11/pybind11.h>

extern double GetFPS(); extern void UpdateFPSCounter();

namespace py = pybind11;

PYBIND11_MODULE(fps_counter, m) { m.def("get_fps", &GetFPS, "Retrieve current FPS"); m.def("update_fps", &UpdateFPSCounter, "Update FPS counter (call after Present)"); } 3. Compilation (MSVC on Windows 11) Requirements:

Microsoft Visual Studio 2022 (with MSVC toolset).

pybind11 installed (pip install pybind11).

Python development headers (ensure Python is installed with dev tools).

Compile commands (Developer Command Prompt):

bash cl /LD fps_counter.cpp pybind_fps.cpp /I"path\to\pybind11\include" /I"path\to\Python\include" /link /OUT:fps_counter.pyd /LIBPATH:"path\to\Python\libs" Output: fps_counter.pyd (Python extension DLL).

  1. DLL Linking & Redistributable The compiled DLL depends on Microsoft Visual C++ Redistributable (matching your MSVC version).

Ensure users install the x64 VC++ Redistributable for Visual Studio 2022.

Without it, Python will throw ImportError: DLL load failed.

  1. Python HUD Integration python import fps_counter

def hud_loop(): while True: fps_counter.update_fps() # call after Present fps = fps_counter.get_fps() print(f"FPS: {fps:.2f}") 6. CUDA/TensorRT Timing (Optional) Use CUDA events for kernel timing:

cpp cudaEvent_t start, stop; cudaEventCreate(&start); cudaEventCreate(&stop);

cudaEventRecord(start, stream); // launch kernel cudaEventRecord(stop, stream); cudaEventSynchronize(stop);

float ms = 0.0f; cudaEventElapsedTime(&ms, start, stop); Expose these timings separately to Python for ML workload profiling.

โœ… Deliverable fps_counter.pyd (Python extension DLL).

Redistributable installer (VC++ 2022 x64).

Documentation for:

Compilation steps.

Python usage.

CUDA/TensorRT timing integration.

๐Ÿ‘‰ With this setup, your HUD will show true FPS measured at DirectX Present, while Python simply displays the value. The VC++ Redistributable ensures deployment works smoothly across Windows 11 machines.