Repository navigation
Expand file tree
/
Copy pathTextureLoaderAsync.cpp
More file actions
239 lines (194 loc) · 9.35 KB
/
Copy pathTextureLoaderAsync.cpp
File metadata and controls
239 lines (194 loc) · 9.35 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "stdafx.h"
#include "TextureLoaderAsync.h"
#include "Log.h"
#include "Core/TextureHashTable.h"
#include "Core/CRC32Manager.h"
#include <windows.h>
#include <d3dx9.h>
namespace ngg { namespace mw { namespace async {
static void IOCPWorkerThread(Context ctx)
{
// Boost thread priority during texture loading for faster startup
HANDLE currentThread = GetCurrentThread();
int originalPriority = GetThreadPriority(currentThread);
SetThreadPriority(currentThread, THREAD_PRIORITY_ABOVE_NORMAL);
while (!ctx.stopLoading->load())
{
DWORD bytesTransferred = 0;
ULONG_PTR completionKey = 0;
LPOVERLAPPED overlapped = nullptr;
BOOL result = GetQueuedCompletionStatus(
*ctx.iocp, &bytesTransferred, &completionKey, &overlapped, 500);
if (!result && overlapped == nullptr)
{
if (ctx.stopLoading->load()) break;
continue;
}
if (completionKey == 0) break; // shutdown signal
if (bytesTransferred == 1)
{
// TPF texture request (DDS in memory)
TPFTextureLoadRequest* req = reinterpret_cast<TPFTextureLoadRequest*>(completionKey);
if (!req) continue;
IDirect3DDevice9** ppDevice = req->ppDevice;
if (!ppDevice) { delete req; continue; }
IDirect3DDevice9* device = *ppDevice; if (!device) { delete req; continue; }
device->AddRef();
IDirect3DTexture9* texture = nullptr;
HRESULT hr;
{
EnterCriticalSection(ctx.d3dxMutex);
hr = D3DXCreateTextureFromFileInMemory(
device, req->ddsData.data(), (UINT)req->ddsData.size(), &texture);
LeaveCriticalSection(ctx.d3dxMutex);
}
device->Release();
if (SUCCEEDED(hr) && texture)
{
// Add to hash table by CRC32
(*ctx.hashTable)->AddTexture(req->hash, texture);
// Also add by all mapped game hashes for this CRC32
EnterCriticalSection(ctx.crc32MapLock);
if (*ctx.crc32ToGameHashMap)
{
auto it = (*ctx.crc32ToGameHashMap)->find(req->hash);
if (it != (*ctx.crc32ToGameHashMap)->end())
{
for (uint32_t gameHash : it->second)
(*ctx.hashTable)->AddTexture(gameHash, texture);
}
}
LeaveCriticalSection(ctx.crc32MapLock);
// Update counters and possibly trigger rebuild
static CRITICAL_SECTION rebuildLock; static bool rebuildInit = false;
if (!rebuildInit) { InitializeCriticalSection(&rebuildLock); rebuildInit = true; }
EnterCriticalSection(&rebuildLock);
int loaded = ctx.texturesLoaded->fetch_add(1) + 1;
int total = ctx.totalTexturesToLoad->load();
int tpfLoaded = ctx.tpfTexturesLoaded->fetch_add(1) + 1;
int tpfTotal = ctx.totalTPFTexturesToLoad->load();
if (loaded % 100 == 0 || loaded == total)
asi_log::Log("CustomTextureLoader: IOCP loading progress: %d/%d textures (%d/%d TPF)", loaded, total, tpfLoaded, tpfTotal);
if (tpfLoaded == tpfTotal && tpfTotal > 0 && ctx.rebuildSwapTable)
{
asi_log::Log("CustomTextureLoader: *** TPF REBUILD TRIGGERED *** All TPF textures loaded (%d/%d)", tpfLoaded, tpfTotal);
ctx.rebuildSwapTable(true);
asi_log::Log("CustomTextureLoader: *** TPF REBUILD COMPLETE *** Textures will be swapped as you drive around");
}
LeaveCriticalSection(&rebuildLock);
}
delete req;
}
else
{
// Regular texture request (load from file path)
TextureLoadRequest* req = reinterpret_cast<TextureLoadRequest*>(completionKey);
if (!req) continue;
IDirect3DDevice9** ppDevice = req->ppDevice;
if (!ppDevice) { asi_log::Log("CustomTextureLoader: ERROR - NULL ppDevice for 0x%08X", req->hash); delete req; continue; }
IDirect3DDevice9* device = *ppDevice;
if (!device) { asi_log::Log("CustomTextureLoader: ERROR - NULL device for 0x%08X", req->hash); delete req; continue; }
device->AddRef();
IDirect3DTexture9* texture = nullptr;
HRESULT hr;
{
EnterCriticalSection(ctx.d3dxMutex);
hr = D3DXCreateTextureFromFileA(device, req->path.c_str(), &texture);
LeaveCriticalSection(ctx.d3dxMutex);
}
device->Release();
if (SUCCEEDED(hr) && texture)
{
uint32_t crc32 = (*ctx.crc32Manager)->CalculateTexmodHash(texture);
if (crc32 != 0)
(*ctx.hashTable)->SetCRC32Hash(req->hash, crc32);
(*ctx.hashTable)->AddTexture(req->hash, texture);
int loaded = ctx.texturesLoaded->fetch_add(1) + 1;
int total = ctx.totalTexturesToLoad->load();
if (loaded % 100 == 0 || loaded == total)
asi_log::Log("CustomTextureLoader: IOCP loading progress: %d/%d textures", loaded, total);
if (loaded == total)
{
asi_log::Log("CustomTextureLoader: All textures loaded - building swap table...");
if (ctx.rebuildSwapTable) ctx.rebuildSwapTable(false);
asi_log::Log("CustomTextureLoader: Textures will be swapped as you drive around");
}
}
else
{
static std::atomic<int> failureCount(0);
int failures = failureCount.fetch_add(1) + 1;
if (failures <= 10 || failures % 100 == 0)
asi_log::Log("CustomTextureLoader: Failed to load texture 0x%08X from '%s' (HRESULT: 0x%08X) - %d failures total",
req->hash, req->path.c_str(), hr, failures);
}
delete req;
}
}
SetThreadPriority(currentThread, originalPriority);
}
bool InitializeWorkers(Context& ctx, DWORD workerCount)
{
if (!ctx.iocp || !ctx.workerThreads) return false;
if (*ctx.iocp && *ctx.workerThreads) return true; // already initialized
*ctx.iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, workerCount);
if (!*ctx.iocp) { asi_log::Log("CustomTextureLoader: Failed to create IOCP!"); return false; }
*ctx.workerThreads = new std::vector<std::thread>();
for (DWORD i = 0; i < workerCount; ++i)
(*ctx.workerThreads)->emplace_back([ctx]() mutable { IOCPWorkerThread(ctx); });
asi_log::Log("CustomTextureLoader: Created IOCP with %lu worker threads", workerCount);
return true;
}
void ShutdownWorkers(Context& ctx)
{
if (!ctx.iocp || !*ctx.iocp) return;
ctx.stopLoading->store(true);
if (ctx.workerThreads && *ctx.workerThreads)
{
for (size_t i = 0; i < (*ctx.workerThreads)->size(); ++i)
PostQueuedCompletionStatus(*ctx.iocp, 0, 0, nullptr);
for (auto& th : **ctx.workerThreads)
if (th.joinable()) th.join();
(*ctx.workerThreads)->clear();
}
CloseHandle(*ctx.iocp);
*ctx.iocp = nullptr;
ctx.stopLoading->store(false);
if (ctx.globalDevice && *ctx.globalDevice)
{
(*ctx.globalDevice)->Release();
*ctx.globalDevice = nullptr;
}
}
void StartIOCPLoading(Context& ctx, IDirect3DDevice9* device, TextureHashTable* hashTable, CRC32Manager* crc32Mgr)
{
if (!device) { asi_log::Log("CustomTextureLoader: Cannot start IOCP loading - device is NULL!"); return; }
if (!ctx.iocp || !*ctx.iocp || !ctx.workerThreads || !*ctx.workerThreads) { asi_log::Log("CustomTextureLoader: IOCP or worker threads not initialized!"); return; }
device->AddRef();
*ctx.globalDevice = device;
ctx.texturesLoaded->store(0);
int pathCount = hashTable->CountTexturePaths();
ctx.totalTexturesToLoad->store(pathCount);
if (ctx.totalTexturesToLoad->load() == 0) { asi_log::Log("CustomTextureLoader: No textures to load"); return; }
int posted = 0;
hashTable->ForEachTexturePath([&](uint32_t hash, const char* path)
{
TextureLoadRequest* req = new TextureLoadRequest{ hash, std::string(path), const_cast<IDirect3DDevice9**>(ctx.globalDevice) };
if (!PostQueuedCompletionStatus(*ctx.iocp, 0, reinterpret_cast<ULONG_PTR>(req), nullptr))
{ asi_log::Log("CustomTextureLoader: Failed to post request for hash 0x%08X", hash); delete req; }
else { posted++; }
});
asi_log::Log("CustomTextureLoader: Posted %d texture loading requests to IOCP queue", posted);
}
bool PostTPFRequest(Context& ctx, uint32_t hash, const std::string& filename, std::vector<uint8_t>&& ddsData)
{
if (!ctx.iocp || !*ctx.iocp) return false;
TPFTextureLoadRequest* req = new TPFTextureLoadRequest{ hash, filename, std::move(ddsData), const_cast<IDirect3DDevice9**>(ctx.globalDevice) };
if (!PostQueuedCompletionStatus(*ctx.iocp, 1, reinterpret_cast<ULONG_PTR>(req), nullptr))
{
asi_log::Log("CustomTextureLoader: Failed to post TPF request for %s", filename.c_str());
delete req; return false;
}
return true;
}
} } } // namespaces