-
-
Notifications
You must be signed in to change notification settings - Fork 508
Expand file tree
/
Copy pathTextureDescrManager.cpp
More file actions
323 lines (272 loc) · 9.66 KB
/
TextureDescrManager.cpp
File metadata and controls
323 lines (272 loc) · 9.66 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "stdafx.h"
#include "TextureDescrManager.h"
#include "ETextureParams.h"
#include "xrCore/Threading/ParallelForEach.hpp"
// eye-params
float r__dtex_range = 50;
class cl_dt_scaler : public R_constant_setup
{
public:
float scale;
cl_dt_scaler(float s) : scale(s) {}
void setup(CBackend& cmd_list, R_constant* C) override { cmd_list.set_c(C, scale, scale, scale, 1 / r__dtex_range); }
};
void fix_texture_thm_name(pstr fn)
{
pstr _ext = strext(fn);
if (_ext && (!xr_stricmp(_ext, ".tga") || !xr_stricmp(_ext, ".thm") || !xr_stricmp(_ext, ".dds") ||
!xr_stricmp(_ext, ".bmp") || !xr_stricmp(_ext, ".ogm")))
{
*_ext = 0;
}
}
void CTextureDescrMngr::LoadLTX(pcstr initial, bool listTHM)
{
string_path fname;
FS.update_path(fname, initial, "textures.ltx");
if (!FS.exist(fname))
return;
#ifndef MASTER_GOLD
Msg("Processing textures.ltx in [%s]", initial);
#endif
CInifile ini(fname);
Lock lock;
if (ini.section_exist("association"))
{
CInifile::Sect& data = ini.r_section("association");
#ifndef MASTER_GOLD
Msg("\tsection [%s] has %d lines", data.Name.c_str(), data.Data.size());
#endif
m_texture_details.reserve(m_texture_details.size() + data.Data.size());
m_detail_scalers.reserve(m_detail_scalers.size() + data.Data.size());
const auto processAssociation = [&](const CInifile::Item& item)
{
if (listTHM)
Msg("\t\t%s = %s", item.name.c_str(), item.value.c_str());
lock.Enter();
texture_desc& desc = m_texture_details[item.name];
cl_dt_scaler*& dts = m_detail_scalers[item.name];
lock.Leave();
if (desc.m_assoc)
xr_delete(desc.m_assoc);
desc.m_assoc = xr_new<texture_assoc>();
string_path T;
float s;
const int res = sscanf(*item.value, "%[^,],%f", T, &s);
R_ASSERT4(res == 2, "Bad texture association", item.name.c_str(), fname);
desc.m_assoc->detail_name = T;
if (dts)
dts->scale = s;
else
dts = xr_new<cl_dt_scaler>(s);
if (strstr(item.value.c_str(), "usage[diffuse_or_bump]"))
desc.m_assoc->usage.set(texture_assoc::flDiffuseDetail | texture_assoc::flBumpDetail);
else if (strstr(item.value.c_str(), "usage[bump]"))
desc.m_assoc->usage.set(texture_assoc::flBumpDetail);
else if (strstr(item.value.c_str(), "usage[diffuse]"))
desc.m_assoc->usage.set(texture_assoc::flDiffuseDetail);
};
xr_parallel_for_each(data.Data, processAssociation);
} // "association"
if (ini.section_exist("specification"))
{
CInifile::Sect& data = ini.r_section("specification");
#ifndef MASTER_GOLD
Msg("\tsection [%s] has %d lines", data.Name.c_str(), data.Data.size());
#endif
m_texture_details.reserve(m_texture_details.size() + data.Data.size());
const auto processSpecification = [&](const CInifile::Item& item)
{
if (listTHM)
Msg("\t\t%s = %s", item.name.c_str(), item.value.c_str());
lock.Enter();
texture_desc& desc = m_texture_details[item.name];
lock.Leave();
if (desc.m_spec)
xr_delete(desc.m_spec);
desc.m_spec = xr_new<texture_spec>();
string_path bmode;
const int res =
sscanf(item.value.c_str(), "bump_mode[%[^]]], material[%f]", bmode, &desc.m_spec->m_material);
R_ASSERT4(res == 2, "Bad texture specification", item.name.c_str(), fname);
if ((bmode[0] == 'u') && (bmode[1] == 's') && (bmode[2] == 'e') && (bmode[3] == ':'))
{
// bump-map specified
desc.m_spec->m_bump_name = bmode + 4;
}
};
xr_parallel_for_each(data.Data, processSpecification);
} // "specification"
}
void CTextureDescrMngr::LoadTHM(LPCSTR initial, bool listTHM)
{
FS_FileSet flist;
FS.file_list(flist, initial, FS_ListFiles, "*.thm");
if (flist.empty())
return;
#ifndef MASTER_GOLD
Msg("Processing %d .thm files in [%s]", flist.size(), initial);
#endif
m_texture_details.reserve(m_texture_details.size() + flist.size());
m_detail_scalers.reserve(m_detail_scalers.size() + flist.size());
Lock lock;
const auto processFile = [&](const FS_File& it)
{
// Alundaio: Print list of *.thm to find bad .thms!
if (listTHM)
Log("\t", it.name.c_str());
string_path fn;
FS.update_path(fn, initial, it.name.c_str());
IReader* F = FS.r_open(fn);
R_ASSERT3(F, "Failed to open THM (case-sensitivity problem?)", it.name.c_str());
xr_strcpy(fn, it.name.c_str());
fix_texture_thm_name(fn);
R_ASSERT3(F->find_chunk(THM_CHUNK_TYPE), "Cannot find THM chunk in file", fn);
F->r_u32();
STextureParams tp;
tp.Load(*F);
FS.r_close(F);
if (STextureParams::ttImage == tp.type || STextureParams::ttTerrain == tp.type ||
STextureParams::ttNormalMap == tp.type)
{
lock.Enter();
texture_desc& desc = m_texture_details[fn];
cl_dt_scaler*& dts = m_detail_scalers[fn];
lock.Leave();
if (tp.detail_name.size() &&
tp.flags.is_any(STextureParams::flDiffuseDetail | STextureParams::flBumpDetail))
{
if (desc.m_assoc)
xr_delete(desc.m_assoc);
desc.m_assoc = xr_new<texture_assoc>();
desc.m_assoc->detail_name = tp.detail_name;
if (dts)
dts->scale = tp.detail_scale;
else
dts = xr_new<cl_dt_scaler>(tp.detail_scale);
if (tp.flags.is(STextureParams::flDiffuseDetail))
desc.m_assoc->usage.set(texture_assoc::flDiffuseDetail);
if (tp.flags.is(STextureParams::flBumpDetail))
desc.m_assoc->usage.set(texture_assoc::flBumpDetail);
}
if (desc.m_spec)
xr_delete(desc.m_spec);
desc.m_spec = xr_new<texture_spec>();
desc.m_spec->m_material = tp.material + tp.material_weight;
desc.m_spec->m_use_steep_parallax = false;
if (tp.bump_mode == STextureParams::tbmUse)
{
desc.m_spec->m_bump_name = tp.bump_name;
}
else if (tp.bump_mode == STextureParams::tbmUseParallax)
{
desc.m_spec->m_bump_name = tp.bump_name;
desc.m_spec->m_use_steep_parallax = true;
}
}
};
if (!listTHM)
xr_parallel_for_each(flist, processFile);
else
{
// We need precise info in the log when listTHM is specified.
// Load in single thread, file by file.
for (const FS_File& file : flist)
processFile(file);
}
}
void CTextureDescrMngr::Load()
{
#ifndef MASTER_GOLD
CTimer timer;
timer.Start();
#endif // #ifdef DEBUG
const bool listTHM = strstr(Core.Params, "-list_thm");
LoadLTX("$game_textures$", listTHM);
LoadLTX("$level$", listTHM);
LoadTHM("$game_textures$", listTHM);
LoadTHM("$level$", listTHM);
#ifndef MASTER_GOLD
Msg("%s, texture descriptions loaded for %d ms", __FUNCTION__, timer.GetElapsed_ms());
#endif
}
void CTextureDescrMngr::UnLoad()
{
for (auto& it : m_texture_details)
{
xr_delete(it.second.m_assoc);
xr_delete(it.second.m_spec);
}
m_texture_details.clear();
}
CTextureDescrMngr::~CTextureDescrMngr()
{
for (auto& it : m_detail_scalers)
xr_delete(it.second);
m_detail_scalers.clear();
}
shared_str CTextureDescrMngr::GetBumpName(const shared_str& tex_name) const
{
map_TD::const_iterator I = m_texture_details.find(tex_name);
if (I != m_texture_details.end())
{
if (I->second.m_spec)
{
return I->second.m_spec->m_bump_name;
}
}
return "";
}
BOOL CTextureDescrMngr::UseSteepParallax(const shared_str& tex_name) const
{
map_TD::const_iterator I = m_texture_details.find(tex_name);
if (I != m_texture_details.end())
{
if (I->second.m_spec)
{
return I->second.m_spec->m_use_steep_parallax;
}
}
return FALSE;
}
float CTextureDescrMngr::GetMaterial(const shared_str& tex_name) const
{
map_TD::const_iterator I = m_texture_details.find(tex_name);
if (I != m_texture_details.end())
{
if (I->second.m_spec)
{
return I->second.m_spec->m_material;
}
}
return 1.0f;
}
void CTextureDescrMngr::GetTextureUsage(const shared_str& tex_name, bool& bDiffuse, bool& bBump) const
{
map_TD::const_iterator I = m_texture_details.find(tex_name);
if (I != m_texture_details.end())
{
if (I->second.m_assoc)
{
auto& usage = I->second.m_assoc->usage;
bDiffuse = usage.test(texture_assoc::flDiffuseDetail);
bBump = usage.test(texture_assoc::flBumpDetail);
}
}
}
BOOL CTextureDescrMngr::GetDetailTexture(const shared_str& tex_name, LPCSTR& res, R_constant_setup*& CS) const
{
map_TD::const_iterator I = m_texture_details.find(tex_name);
if (I != m_texture_details.end())
{
if (I->second.m_assoc)
{
texture_assoc* TA = I->second.m_assoc;
res = TA->detail_name.c_str();
map_CS::const_iterator It2 = m_detail_scalers.find(tex_name);
CS = It2 == m_detail_scalers.end() ? 0 : It2->second;
return TRUE;
}
}
return FALSE;
}