-
Notifications
You must be signed in to change notification settings - Fork 83
/
d3d8to9.cpp
188 lines (157 loc) · 4.84 KB
/
d3d8to9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Copyright (C) 2015 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/d3d8to9#license
*/
#include "d3dx9.hpp"
#include "d3d8to9.hpp"
PFN_D3DXAssembleShader D3DXAssembleShader = nullptr;
PFN_D3DXDisassembleShader D3DXDisassembleShader = nullptr;
PFN_D3DXLoadSurfaceFromSurface D3DXLoadSurfaceFromSurface = nullptr;
#ifndef D3D8TO9NOLOG
// Very simple logging for the purpose of debugging only.
std::ofstream LOG;
#endif
extern "C" HRESULT WINAPI ValidatePixelShader(const DWORD* pPixelShader, const D3DCAPS8* pCaps, BOOL ReturnErrors, char** pErrorsString)
{
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "ValidatePixelShader " << "(" << pPixelShader << ", " << pCaps << ", " << ReturnErrors << ", " << pErrorsString << ")' ..." << std::endl;
#endif
HRESULT hr = E_FAIL;
const char* errorMessage = "";
if (!pPixelShader)
{
errorMessage = "Invalid code pointer.\n";
}
else
{
switch (*pPixelShader)
{
case D3DPS_VERSION(1, 0):
case D3DPS_VERSION(1, 1):
case D3DPS_VERSION(1, 2):
case D3DPS_VERSION(1, 3):
case D3DPS_VERSION(1, 4):
if (pCaps && *pPixelShader > pCaps->PixelShaderVersion)
{
errorMessage = "Shader version not supported by caps.\n";
break;
}
hr = S_OK;
break;
default:
errorMessage = "Unsupported shader version.\n";
}
}
if (!ReturnErrors)
{
errorMessage = "";
}
if (pErrorsString)
{
const size_t size = strlen(errorMessage) + 1;
*pErrorsString = (char*) HeapAlloc(GetProcessHeap(), 0, size);
if (*pErrorsString)
{
memcpy(*pErrorsString, errorMessage, size);
}
}
return hr;
}
extern "C" HRESULT WINAPI ValidateVertexShader(const DWORD* pVertexShader, const DWORD* pVertexDecl, const D3DCAPS8* pCaps, BOOL ReturnErrors, char** pErrorsString)
{
UNREFERENCED_PARAMETER(pVertexDecl);
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "ValidateVertexShader " << "(" << pVertexShader << ", " << pVertexDecl << ", " << pCaps << ", " << ReturnErrors << ", " << pErrorsString << ")' ..." << std::endl;
#endif
HRESULT hr = E_FAIL;
const char* errorMessage = "";
if (!pVertexShader)
{
errorMessage = "Invalid code pointer.\n";
}
else
{
switch (*pVertexShader)
{
case D3DVS_VERSION(1, 0):
case D3DVS_VERSION(1, 1):
if (pCaps && *pVertexShader > pCaps->VertexShaderVersion)
{
errorMessage = "Shader version not supported by caps.\n";
break;
}
hr = S_OK;
break;
default:
errorMessage = "Unsupported shader version.\n";
}
}
if (!ReturnErrors)
{
errorMessage = "";
}
if (pErrorsString)
{
const size_t size = strlen(errorMessage) + 1;
*pErrorsString = (char*) HeapAlloc(GetProcessHeap(), 0, size);
if (*pErrorsString)
{
memcpy(*pErrorsString, errorMessage, size);
}
}
return hr;
}
extern "C" void WINAPI DebugSetMute()
{
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "DebugSetMute ()" << "'..." << std::endl;
#endif
}
extern "C" IDirect3D8 *WINAPI Direct3DCreate8(UINT SDKVersion)
{
#ifndef D3D8TO9NOLOG
static bool LogMessageFlag = true;
if (!LOG.is_open())
{
LOG.open("d3d8.log", std::ios::trunc);
}
if (!LOG.is_open() && LogMessageFlag)
{
LogMessageFlag = false;
MessageBox(nullptr, TEXT("Failed to open debug log file \"d3d8.log\"!"), nullptr, MB_ICONWARNING);
}
LOG << "Redirecting '" << "Direct3DCreate8" << "(" << SDKVersion << ")' ..." << std::endl;
LOG << "> Passing on to 'Direct3DCreate9':" << std::endl;
#endif
IDirect3D9 *const d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (d3d == nullptr)
{
return nullptr;
}
// Load D3DX
if (!D3DXAssembleShader || !D3DXDisassembleShader || !D3DXLoadSurfaceFromSurface)
{
const HMODULE module = LoadLibrary(TEXT("d3dx9_43.dll"));
if (module != nullptr)
{
D3DXAssembleShader = reinterpret_cast<PFN_D3DXAssembleShader>(GetProcAddress(module, "D3DXAssembleShader"));
D3DXDisassembleShader = reinterpret_cast<PFN_D3DXDisassembleShader>(GetProcAddress(module, "D3DXDisassembleShader"));
D3DXLoadSurfaceFromSurface = reinterpret_cast<PFN_D3DXLoadSurfaceFromSurface>(GetProcAddress(module, "D3DXLoadSurfaceFromSurface"));
}
else
{
#ifndef D3D8TO9NOLOG
LOG << "Failed to load d3dx9_43.dll! Some features will not work correctly." << std::endl;
#endif
if (MessageBox(nullptr, TEXT(
"Failed to load d3dx9_43.dll! Some features will not work correctly.\n\n"
"It's required to install the \"Microsoft DirectX End-User Runtime\" in order to use d3d8to9, or alternatively get the DLLs from this NuGet package:\nhttps://www.nuget.org/packages/Microsoft.DXSDK.D3DX\n\n"
"Please click \"OK\" to open the official download page or \"Cancel\" to continue anyway."), nullptr, MB_ICONWARNING | MB_TOPMOST | MB_SETFOREGROUND | MB_OKCANCEL | MB_DEFBUTTON1) == IDOK)
{
ShellExecute(nullptr, TEXT("open"), TEXT("https://www.microsoft.com/download/details.aspx?id=35"), nullptr, nullptr, SW_SHOW);
return nullptr;
}
}
}
return new Direct3D8(d3d);
}