Skip to content

Commit

Permalink
various experimental texture stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterTh committed Feb 28, 2016
1 parent 44a543e commit b276be5
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 46 deletions.
5 changes: 3 additions & 2 deletions GeDoSaTo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<TargetMachine>MachineX86</TargetMachine>
<ModuleDefinitionFile>
</ModuleDefinitionFile>
<AdditionalDependencies>d3d11.lib;d3dx11.lib;D3DCompiler.lib;Effects11.lib;legacy_stdio_definitions.lib;nvapi.lib;Xinput.lib;libMinHook.x86.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;d3d11.lib;d3dx11.lib;D3DCompiler.lib;Effects11.lib;legacy_stdio_definitions.lib;nvapi.lib;Xinput.lib;libMinHook.x86.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<IgnoreSpecificDefaultLibraries>
</IgnoreSpecificDefaultLibraries>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down Expand Up @@ -162,7 +162,7 @@
</ClCompile>
<Link>
<SuppressStartupBanner>true</SuppressStartupBanner>
<AdditionalDependencies>d3d11.lib;d3dx11.lib;D3DCompiler.lib;Effects11.lib;legacy_stdio_definitions.lib;nvapi64.lib;Xinput.lib;libMinHook.x64.lib;winmm.lib;psapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>opengl32.lib;d3d11.lib;d3dx11.lib;D3DCompiler.lib;Effects11.lib;legacy_stdio_definitions.lib;nvapi64.lib;Xinput.lib;libMinHook.x64.lib;winmm.lib;psapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Windows</SubSystem>
<MinimumRequiredVersion>
</MinimumRequiredVersion>
Expand Down Expand Up @@ -296,6 +296,7 @@
<ClCompile Include="source\dxgi\dxgidevice3.cpp" />
<ClCompile Include="source\dxgi\dxgifactory2.cpp" />
<ClCompile Include="source\dxgi\dxgifactory3.cpp" />
<ClCompile Include="source\ogl\opengl_detouring.cpp" />
<ClCompile Include="source\perf_monitor.cpp" />
<ClCompile Include="source\plugins\dw8.cpp" />
<ClCompile Include="source\plugins\ff13.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions defs/hooks.def
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ HOOK(timeGetTime, "Winmm.dll")

HOOK(SteamFriends, "steam_api.dll")

HOOK(glTexImage2D, "OPENGL32.dll")
HOOK(glPixelMapfv, "OPENGL32.dll")

//HOOK(GetProcAddress, "Kernel32.dll")

#undef HOOK
2 changes: 2 additions & 0 deletions include/d3d9/d3d9tex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const extern GUID IID_GedosatoTexture;
interface hkIDirect3DTexture9 : public IDirect3DTexture9 {
IDirect3DTexture9 *m_pWrapped;
bool dirty = false;
D3DLOCKED_RECT* lockedRect = nullptr;
RECT rect;

public:
hkIDirect3DTexture9(IDirect3DTexture9 **ppIDirect3DTexture9);
Expand Down
1 change: 1 addition & 0 deletions include/renderstate_manager_dx9.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class RSManagerDX9 : public RSManager {
HRESULT redirectGetDepthStencilSurface(IDirect3DSurface9 ** ppZStencilSurface);
void redirectSetCursorPosition(int X, int Y, DWORD Flags);

HRESULT redirectSetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture);
HRESULT redirectSetPixelShader(IDirect3DPixelShader9* pShader);
HRESULT redirectSetVertexShader(IDirect3DVertexShader9* pvShader);
HRESULT redirectSetRenderState(D3DRENDERSTATETYPE State, DWORD Value);
Expand Down
Binary file modified pack/GeDoSaTo.dll
Binary file not shown.
1 change: 1 addition & 0 deletions pack/config/whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ ffxiv* || Final Fantasy XIV Launcher or Configurator
FirefallClient || Firefall
FoxGame-Win32-Shipping || Blacklight: Retribution
fr-041_debris || "Debris" by Farbrausch
Grim Dawn || Grim Dawn
grimrock || Legend of Grimrock
grimrock2 || Legend of Grimrock 2
GSGameExe_DX9 || Giana Sisters: Twisted Dreams
Expand Down
Binary file modified pack/gedoshim.dll
Binary file not shown.
18 changes: 1 addition & 17 deletions source/d3d9/d3d9dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,23 +660,7 @@ HRESULT APIENTRY hkIDirect3DDevice9::SetStreamSourceFreq(UINT StreamNumber,UINT
HRESULT APIENTRY hkIDirect3DDevice9::SetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture) {
RSManager::setLatest(rsMan);
SDLOG(6, "setTexture %d, %p\n", Stage, pTexture);
if(pTexture != NULL && Settings::get().getEnableAlternativeTextureDumping()) {
void *unused;
if(pTexture->QueryInterface(IID_GedosatoTexture, &unused) == S_OK) {
pTexture = reinterpret_cast<hkIDirect3DTexture9*>(pTexture)->m_pWrapped;
SDLOG(6, " - wrapper for %p\n", pTexture);
}
}
if(Settings::get().getLogLevel() > 10 && pTexture) {
IDirect3DTexture9 *tex;
if(pTexture->QueryInterface(IID_IDirect3DTexture9, (void**)&tex) == S_OK) {
D3DSURFACE_DESC desc;
tex->GetLevelDesc(0, &desc);
SDLOG(10, " -- size: %dx%d RT? %s\n", desc.Width, desc.Height, (desc.Usage & D3DUSAGE_RENDERTARGET) ? "true" : "false");
tex->Release();
}
}
return m_pD3Ddev->SetTexture(Stage, pTexture);
return rsMan->redirectSetTexture(Stage, pTexture);
}

HRESULT APIENTRY hkIDirect3DDevice9::SetTextureStageState(DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
Expand Down
111 changes: 109 additions & 2 deletions source/d3d9/d3d9tex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "d3d9/d3d9tex.h"

#include "settings.h"
#include "utils/win_utils.h"
#include "utils/d3d9_utils.h"
#include "utils/imgproc_utils.h"
#include "renderstate_manager_dx9.h"


IDirect3DTexture9* hkIDirect3DTexture9::getTextureToUse() {
Expand Down Expand Up @@ -123,14 +127,117 @@ HRESULT APIENTRY hkIDirect3DTexture9::GetSurfaceLevel(UINT Level, IDirect3DSurfa
return m_pWrapped->GetSurfaceLevel(Level, ppSurfaceLevel);
}


namespace {
void convertToARGB8(const void* in, void* out, int width, int height, int inPitch, D3DFORMAT fmt) {
if(fmt == D3DFORMAT::D3DFMT_A8R8G8B8) {
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
((uint32_t*)out)[0 + x + y*width] = ((uint32_t*)in)[0 + x + y*inPitch/4];
}
}
}
//else if(fmt == D3DFORMAT::D3DFMT_A8R8G8B8) {
// for(int y = 0; y < height; ++y) {
// for(int x = 0; x < width; ++x) {
// ((BYTE*)out)[0 + x * 4 + y*width * 4] = ((BYTE*)in)[0 + x * 4 + y*inPitch];
// ((BYTE*)out)[1 + x * 4 + y*width * 4] = ((BYTE*)in)[3 + x * 4 + y*inPitch];
// ((BYTE*)out)[2 + x * 4 + y*width * 4] = ((BYTE*)in)[2 + x * 4 + y*inPitch];
// ((BYTE*)out)[3 + x * 4 + y*width * 4] = ((BYTE*)in)[1 + x * 4 + y*inPitch];
// }
// }
//}
else {
SDLOG(-1, "ERROR: texture scaling conversion - unsupported format: %s", D3DFormatToString(fmt));
}
}
void convertFromARGB8(const void* in, void* out, int width, int height, int outPitch, D3DFORMAT fmt) {
if(fmt == D3DFORMAT::D3DFMT_A8R8G8B8) {
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
((uint32_t*)out)[0 + x + y*outPitch/4] = ((uint32_t*)in)[0 + x + y*width];
}
}
}
//else if(fmt == D3DFORMAT::D3DFMT_A8R8G8B8) {
// for(int y = 0; y < height; ++y) {
// for(int x = 0; x < width; ++x) {
// ((BYTE*)out)[0 + x * 4 + y*outPitch] = ((BYTE*)in)[0 + x * 4 + y*width * 4];
// ((BYTE*)out)[1 + x * 4 + y*outPitch] = ((BYTE*)in)[3 + x * 4 + y*width * 4];
// ((BYTE*)out)[2 + x * 4 + y*outPitch] = ((BYTE*)in)[2 + x * 4 + y*width * 4];
// ((BYTE*)out)[3 + x * 4 + y*outPitch] = ((BYTE*)in)[1 + x * 4 + y*width * 4];
// }
// }
//}
else {
SDLOG(-1, "ERROR: texture scaling inverse conversion - unsupported format: %s", D3DFormatToString(fmt));
}
}
}

HRESULT APIENTRY hkIDirect3DTexture9::LockRect(UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
SDLOG(20, "hkIDirect3DTexture9::LockRect\n");
dirty = true;
return m_pWrapped->LockRect(Level, pLockedRect, pRect, Flags);

D3DSURFACE_DESC desc;
GetLevelDesc(Level, &desc);
if(pRect != nullptr) {
rect = *pRect;
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
rect.left *= Settings::get().getTextureScalingFactor();
rect.right = rect.left + width;
rect.top *= Settings::get().getTextureScalingFactor();
rect.bottom = rect.top + height;
} else {
rect.left = 0;
rect.top = 0;
rect.right = desc.Width / Settings::get().getTextureScalingFactor();
rect.bottom = desc.Height / Settings::get().getTextureScalingFactor();
}
auto ret = m_pWrapped->LockRect(Level, pLockedRect, &rect, Flags);
if(SUCCEEDED(ret)) {
dirty = true;
lockedRect = pLockedRect;
SDLOG(-1, " -> locked rect %s on texture size %4dx%4d\n", RectToString(&rect), desc.Width, desc.Height);
}
return ret;
}

HRESULT APIENTRY hkIDirect3DTexture9::UnlockRect(UINT Level) {
SDLOG(20, "hkIDirect3DTexture9::UnlockRect\n");
if(lockedRect != nullptr) {
SDLOG(-1, " -> unlocking, scaling\n");
D3DSURFACE_DESC desc;
GetLevelDesc(Level, &desc);
SDLOG(-1, " (tex format: %s)\n", D3DSurfaceDescToString(desc));
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
size_t bytesize = width*height*4;
SDLOG(-1, " -> allocating %d\n", bytesize);
BYTE* data = new BYTE[bytesize];
uint32_t* uintData = (uint32_t*)data;
SDLOG(-1, " -> allocated\n");
convertToARGB8(lockedRect->pBits, uintData, width, height, lockedRect->Pitch, desc.Format);
SDLOG(-1, " -> copied\n");
//stbi_write_png(format(R"(C:\tmp\dump\%p_unscaled.png)",m_pWrapped).c_str(), width, height, 4, data, width*4);
auto ret = m_pWrapped->UnlockRect(Level);
SDLOG(-1, " -> scaling\n");
GlobalTexScaler::get().Scale(uintData, width, height, Settings::get().getTextureScalingFactor());
D3DLOCKED_RECT relocked;
rect.right = rect.left + width;
rect.bottom = rect.top + height;
if(SUCCEEDED(m_pWrapped->LockRect(Level, &relocked, &rect, 0))) {
SDLOG(-1, " -> relocked rect %s pitch %d\n", RectToString(&rect), relocked.Pitch);
convertFromARGB8(uintData, relocked.pBits, width, height, relocked.Pitch, desc.Format);
SDLOG(-1, " -> copied back\n");
//stbi_write_png(format(R"(C:\tmp\dump\%p_scaled.png)", m_pWrapped).c_str(), width, height, 4, uintData, width * 4);
m_pWrapped->UnlockRect(Level);
}
SDLOG(-1, " -> cleaning data\n");
delete [] data;
lockedRect = nullptr;
return ret;
}
return m_pWrapped->UnlockRect(Level);
}

Expand Down
7 changes: 7 additions & 0 deletions source/detouring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "steam/steamfriends.h"

#include <gl/GL.h>

#define GENERATE_INTERCEPT_HEADER(__name, __rettype, __convention, ...) \
typedef __rettype (__convention * __name##_FNType)(__VA_ARGS__); \
__name##_FNType True##__name, __name##Pointer; \
Expand Down Expand Up @@ -742,6 +744,11 @@ GENERATE_INTERCEPT_HEADER(SteamFriends, ISteamFriends*, WINAPI) {
return TrueSteamFriends();
}

// OpenGL ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

GENERATE_INTERCEPT_HEADER(glTexImage2D, void, WINAPI, GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid * data);
GENERATE_INTERCEPT_HEADER(glPixelMapfv, void, WINAPI, GLenum map, GLsizei mapsize, const GLfloat * values);

// GetProcAddress ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

GENERATE_INTERCEPT_HEADER(GetProcAddress, FARPROC, WINAPI, _In_ HMODULE hModule, _In_ LPCSTR lpProcName) {
Expand Down
31 changes: 31 additions & 0 deletions source/renderstate_manager_dx9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "d3d9/d3d9dev_ex.h"
#include "d3d9/d3d9swap.h"
#include "d3d9/d3d9tex.h"
#include "utils/d3d9_utils.h"
#include "key_actions.h"
#include "detouring.h"
Expand Down Expand Up @@ -828,6 +829,29 @@ void RSManagerDX9::redirectSetCursorPosition(int X, int Y, DWORD Flags) {
d3ddev->SetCursorPosition(X, Y, Flags);
}

HRESULT RSManagerDX9::redirectSetTexture(DWORD Stage, IDirect3DBaseTexture9 *pTexture) {
if(pTexture != NULL && (Settings::get().getEnableAlternativeTextureDumping() || Settings::get().getTextureScalingFactor() > 1)) {
void *unused;
if(pTexture->QueryInterface(IID_GedosatoTexture, &unused) == S_OK) {
pTexture = reinterpret_cast<hkIDirect3DTexture9*>(pTexture)->m_pWrapped;
SDLOG(6, " - wrapper for %p\n", pTexture);
}
}
if(Settings::get().getLogLevel() > 10 && pTexture) {
IDirect3DTexture9 *tex;
if(pTexture->QueryInterface(IID_IDirect3DTexture9, (void**)&tex) == S_OK) {
D3DSURFACE_DESC desc;
tex->GetLevelDesc(0, &desc);
SDLOG(10, " -- size: %dx%d RT? %s\n", desc.Width, desc.Height, (desc.Usage & D3DUSAGE_RENDERTARGET) ? "true" : "false");
if(dumpingFrame) {
dumpTexture(format("framedump_settexture%03u_stage%d_texture_%p", renderTargetSwitches++, Stage, pTexture).c_str(), tex);
}
tex->Release();
}
}
return d3ddev->SetTexture(Stage, pTexture);
}

HRESULT RSManagerDX9::redirectSetPixelShader(IDirect3DPixelShader9* pShader) {
if(dumpingFrame) {
IDirect3DSurface9* rt;
Expand Down Expand Up @@ -875,6 +899,13 @@ HRESULT RSManagerDX9::redirectSetPixelShaderConstantF(UINT StartRegister, CONST
}

HRESULT RSManagerDX9::redirectCreateTexture(UINT Width, UINT Height, UINT Levels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, HANDLE* pSharedHandle) {
if((Settings::get().getEnableAlternativeTextureDumping() || Settings::get().getTextureScalingFactor()>1) && !(Usage & D3DUSAGE_RENDERTARGET) && Width > 1 && Height > 1) {
HRESULT hr = plugin->redirectCreateTexture(Width*Settings::get().getTextureScalingFactor(), Height*Settings::get().getTextureScalingFactor(), Levels, Usage, Format, Pool, ppTexture, pSharedHandle);
if(SUCCEEDED(hr)) {
new hkIDirect3DTexture9(ppTexture);
}
return hr;
}
return plugin->redirectCreateTexture(Width, Height, Levels, Usage, Format, Pool, ppTexture, pSharedHandle);
}

Expand Down
41 changes: 19 additions & 22 deletions source/utils/imgproc_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,6 @@ bool TextureScaler::IsEmptyOrFlat(u32* data, int pixels) {
}

void TextureScaler::Scale(u32* &data, int &width, int &height, int factor) {
// prevent processing empty or flat textures (this happens a lot in some games)
// doesn't hurt the standard case, will be very quick for textures with actual texture
if(IsEmptyOrFlat(data, width*height)) {
SDLOG(20, "TextureScaler: early exit -- empty/flat texture");
return;
}

#ifdef SCALING_MEASURE_TIME
double t_start = real_time_now();
#endif
Expand All @@ -515,21 +508,25 @@ void TextureScaler::Scale(u32* &data, int &width, int &height, int factor) {
}

// scale
switch(Settings::get().getTextureScalingMode()) {
case XBRZ:
ScaleXBRZ(factor, inputBuf, outputBuf, width, height);
break;
case HYBRID:
ScaleHybrid(factor, inputBuf, outputBuf, width, height);
break;
case BICUBIC:
ScaleBicubicMitchell(factor, inputBuf, outputBuf, width, height);
break;
case HYBRID_BICUBIC:
ScaleHybrid(factor, inputBuf, outputBuf, width, height, true);
break;
default:
SDLOG(-1, "Unknown scaling type: %d", Settings::get().getTextureScalingMode());
if(factor == 1) {
memcpy(outputBuf, inputBuf, 4*width*height);
} else {
switch(Settings::get().getTextureScalingMode()) {
case XBRZ:
ScaleXBRZ(factor, inputBuf, outputBuf, width, height);
break;
case HYBRID:
ScaleHybrid(factor, inputBuf, outputBuf, width, height);
break;
case BICUBIC:
ScaleBicubicMitchell(factor, inputBuf, outputBuf, width, height);
break;
case HYBRID_BICUBIC:
ScaleHybrid(factor, inputBuf, outputBuf, width, height, true);
break;
default:
SDLOG(-1, "Unknown scaling type: %d", Settings::get().getTextureScalingMode());
}
}

// update values accordingly
Expand Down
2 changes: 1 addition & 1 deletion source/utils/win_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const char* GetDeviceCapsParamToString(int index) {

string RectToString(const RECT* rect) {
if(!rect) return string("NULL_RECT");
return format("RECT[%4ld/%4ld/%4ld/%4ld]", rect->left, rect->top, rect->right, rect->bottom);
return format("RECT[%4ld..%4ld/%4ld..%4ld]", rect->left, rect->right, rect->top, rect->bottom);
}

DWORD RunSilent(const char* command) {
Expand Down
4 changes: 2 additions & 2 deletions source/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const char* VER_NAME = "How Could Hell Be Any Worse";

const unsigned VER_MAJOR = 0;
const unsigned VER_MINOR = 21;
const unsigned VER_BUILD = 2160;
const unsigned VER_BUILD = 2255;

const char* VER_STRING = "version 0.21.2160 built on 2016/02/15 20:19:50";
const char* VER_STRING = "version 0.21.2255 built on 2016/02/28 20:21:17";

#ifdef _DEBUG
const char* MODE_STRING = "DEBUG";
Expand Down

0 comments on commit b276be5

Please sign in to comment.