Skip to content

Commit

Permalink
Fixed HUD issues, improved FPS limiter+threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterTh committed Sep 29, 2012
1 parent b844b9d commit 1598e4b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 15 deletions.
5 changes: 5 additions & 0 deletions DATA/DSfix.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ unlockFPS 0
# do not set this much higher than 60, this will lead to various issues with the engine
FPSlimit 60

# FPS threshold
# DSfix will dynamically disable SMAA if your framerate drops below this value
# and re-enable it once it has normalized (with a bit of hysteresis thresholding)
FPSthreshold 45

############# Depth of field

# Depth of Field resolution override, possible values:
Expand Down
15 changes: 15 additions & 0 deletions DATA/VERSIONS.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
29-09-1012 -- version 1.7 beta
==============================
- Re-implemented the FPS limiter. It's quite exact now and also performed at a better point in time
(before presenting the frame)
- Made frame period calculation (for animation) slightly more exact
- Added FPS thresholding for SMAA


29-09-1012 -- version 1.6.1 beta
================================
- Fixed sporadic graphics issues introduced in 1.6
- Fixed modified HUD issues with very long spell names
- Fixed modified HUD issues with "POISONED"/"BLOOD LOSS" text


29-09-1012 -- version 1.6 beta
==============================
- Unlocked framerate. Thanks to the hard work of Cl�ment Barnier, the framerate can now be unlocked entirely.
Expand Down
15 changes: 5 additions & 10 deletions FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ static DWORD HighGraphics;
static DWORD TaskFuncPtr;
static LARGE_INTEGER timerFreq;
static LARGE_INTEGER counterAtStart;
static unsigned int lastTime, currentTime, deltaTime;

//----------------------------------------------------------------------------------------
//Functions
Expand Down Expand Up @@ -68,10 +67,10 @@ void updateAnimationStepTime(float stepTime, float minFPS, float maxFPS) {
}

// Timer
unsigned int GetElapsedTime(void) {
float getElapsedTime(void) {
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return (unsigned int)( (c.QuadPart - counterAtStart.QuadPart) * 1000 / timerFreq.QuadPart );
return (float)( (c.QuadPart - counterAtStart.QuadPart) * 1000.0f / (float)timerFreq.QuadPart );
}

// Detour
Expand Down Expand Up @@ -138,7 +137,7 @@ void renderLoop(void) {
float minFPS = 10.0f;
QueryPerformanceFrequency(&timerFreq);
QueryPerformanceCounter(&counterAtStart);
lastTime = 0;
float lastTime = 0.0f;
unsigned int minStep = (unsigned int)(1.0f/maxFPS*1000);

// Render loop
Expand Down Expand Up @@ -233,12 +232,8 @@ void renderLoop(void) {

// If rendering was performed, update animation step-time
if((taskFunc == 2) || (taskFunc == 5)) {
currentTime = GetElapsedTime();
deltaTime = currentTime - lastTime;

// Dirty frame-rate cap
if(deltaTime < minStep)
Sleep(minStep - deltaTime);
float currentTime = getElapsedTime();
float deltaTime = currentTime - lastTime;

// Update step-time
updateAnimationStepTime((float)deltaTime, minFPS, maxFPS);
Expand Down
3 changes: 3 additions & 0 deletions FPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
#pragma once

void applyFPSPatch();

// returns time since startup in milliseconds
float getElapsedTime(void);
4 changes: 2 additions & 2 deletions Hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ void HUD::go(IDirect3DTexture9 *input, IDirect3DSurface9 *dst) {
effect->SetFloat(opacityHandle, 1.0f);
effect->Begin(&passes, 0);
effect->BeginPass(0);
rect(0.35f, 0.3f, 0.4f, 0.4f,
0.35f + 0.15f*iscale, 0.30f + 0.15f*iscale, 0.4f*scale, 0.4f*scale);
rect(0.37f, 0.22f, 0.4f, 0.5f,
0.37f + 0.15f*iscale, 0.22f + 0.15f*iscale, 0.4f*scale, 0.5f*scale);
effect->EndPass();
effect->End();

Expand Down
31 changes: 30 additions & 1 deletion RenderstateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Hash.h"
#include "Detouring.h"
#include "WindowManager.h"
#include "FPS.h"

RSManager RSManager::instance;

Expand Down Expand Up @@ -68,6 +69,8 @@ HRESULT RSManager::redirectPresent(CONST RECT *pSourceRect, CONST RECT *pDestRec
mainRT = NULL;
mainRTuses = 0;
zSurf = NULL;

frameTimeManagement();
//if(Settings::get().getEnableTripleBuffering()) return ((IDirect3DDevice9Ex*)d3ddev)->PresentEx(NULL, NULL, NULL, NULL, D3DPRESENT_FORCEIMMEDIATE);
return d3ddev->Present(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
}
Expand Down Expand Up @@ -239,7 +242,7 @@ HRESULT RSManager::redirectSetRenderTarget(DWORD RenderTargetIndex, IDirect3DSur
oldRenderTarget->Release();
}

if(mainRTuses == 5 && mainRT && zSurf && (smaa && doSmaa)) { // time to do SMAA
if(mainRTuses == 5 && !lowFPSmode && mainRT && zSurf && (smaa && doSmaa)) { // time to do SMAA
IDirect3DSurface9 *oldRenderTarget;
d3ddev->GetRenderTarget(0, &oldRenderTarget);
if(oldRenderTarget == mainRT) {
Expand All @@ -250,6 +253,13 @@ HRESULT RSManager::redirectSetRenderTarget(DWORD RenderTargetIndex, IDirect3DSur
oldRenderTarget->GetDesc(&desc);
if(desc.Width == Settings::get().getRenderWidth() && desc.Height == Settings::get().getRenderHeight()) {
storeRenderState();
d3ddev->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
//d3ddev->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
//d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
//d3ddev->SetRenderState(D3DRS_STENCILENABLE, FALSE);
//d3ddev->SetRenderState(D3DRS_CLIPPING, FALSE);
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
d3ddev->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE);
// perform SMAA processing
if(smaa && doSmaa) {
smaa->go(tex, tex, rgbaBuffer1Surf, SMAA::INPUT_LUMA);
Expand Down Expand Up @@ -784,3 +794,22 @@ HRESULT RSManager::redirectSetRenderState(D3DRENDERSTATETYPE State, DWORD Value)
//}
//return D3D_OK;
}

void RSManager::frameTimeManagement() {
float renderTime = getElapsedTime() - lastPresentTime;

// implement FPS threshold
float thresholdRenderTime = (1000.0f / Settings::get().getFPSThreshold()) + 0.2f;
if(renderTime > thresholdRenderTime) lowFPSmode = true;
else if(renderTime < thresholdRenderTime - 1.0f) lowFPSmode = false;

// implement FPS cap
if(Settings::get().getUnlockFPS()) {
float desiredRenderTime = (1000.0f / Settings::get().getFPSLimit()) - 0.2f;
while(renderTime < desiredRenderTime) {
SwitchToThread();
renderTime = getElapsedTime() - lastPresentTime;
}
lastPresentTime = getElapsedTime();
}
}
6 changes: 6 additions & 0 deletions RenderstateManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class RSManager {
D3DVIEWPORT9 viewport;
IDirect3DDevice9 *d3ddev;

float lastPresentTime;
bool lowFPSmode;

bool doSmaa;
SMAA* smaa;

Expand Down Expand Up @@ -158,6 +161,9 @@ class RSManager {
HRESULT redirectSetTexture(DWORD Stage, IDirect3DBaseTexture9 * pTexture);
HRESULT redirectSetDepthStencilSurface(IDirect3DSurface9* pNewZStencil);
HRESULT redirectPresent(CONST RECT * pSourceRect, CONST RECT * pDestRect, HWND hDestWindowOverride, CONST RGNDATA * pDirtyRegion);

void frameTimeManagement();

HRESULT redirectDrawIndexedPrimitiveUP(D3DPRIMITIVETYPE PrimitiveType, UINT MinIndex, UINT NumVertices, UINT PrimitiveCount, CONST void* pIndexData, D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride);
HRESULT redirectDrawPrimitiveUP(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride);
HRESULT redirectD3DXCreateTextureFromFileInMemoryEx(LPDIRECT3DDEVICE9 pDevice, LPCVOID pSrcData, UINT SrcDataSize, UINT Width, UINT Height, UINT MipLevels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, DWORD Filter, DWORD MipFilter, D3DCOLOR ColorKey, D3DXIMAGE_INFO* pSrcInfo, PALETTEENTRY* pPalette, LPDIRECT3DTEXTURE9* ppTexture);
Expand Down
3 changes: 2 additions & 1 deletion Settings.def
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ SETTING(unsigned, SsaoStrength, "ssaoStrength", 0);

SETTING(bool, UnlockFPS, "unlockFPS", 0);
SETTING(unsigned, FPSLimit, "FPSlimit", 30);
SETTING(unsigned, FPSThreshold, "FPSthreshold", 29);

SETTING(bool, EnableTripleBuffering, "enableTripleBuffering", 0);

// Logging Options
SETTING(unsigned, LogLevel, "logLevel", 10);
SETTING(unsigned, LogLevel, "logLevel", 0);

// Cursor Options
SETTING(bool, DisableCursor, "disableCursor", false);
Expand Down
2 changes: 1 addition & 1 deletion main.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#define VERSION "1.6"
#define VERSION "1.7"

#define RELEASE_VER

Expand Down

0 comments on commit 1598e4b

Please sign in to comment.