Skip to content

Commit

Permalink
Added SOC style texture descriptions support (#392)
Browse files Browse the repository at this point in the history
And refactored TextureDescrManager a bit.
  • Loading branch information
Xottab-DUTY committed May 10, 2019
1 parent aec38d6 commit 50a4548
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 24 deletions.
134 changes: 111 additions & 23 deletions src/Layers/xrRender/TextureDescrManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,120 @@ void fix_texture_thm_name(LPSTR fn)
}

#ifdef SINGLETHREADED_TEXTURES_DESCR
#define LOCK_DESCR(lock)
#define UNLOCK_DESCR(lock)
#define DECLARE_DESCR_LOCK
#define LOCK_DESCR()
#define UNLOCK_DESCR()
#define PROCESS_DESCR(processable, function) \
for (const auto& item : processable) \
function(item)
#else
#define LOCK_DESCR(lock) lock.Enter()
#define UNLOCK_DESCR(lock) lock.Leave()
#define DECLARE_DESCR_LOCK Lock descrLock
#define LOCK_DESCR() descrLock.Enter()
#define UNLOCK_DESCR() descrLock.Leave()
#define PROCESS_DESCR(processable, function) \
tbb::parallel_for_each(processable, function)
#endif

void CTextureDescrMngr::LoadTHM(LPCSTR initial)
void CTextureDescrMngr::LoadLTX(pcstr initial, bool listTHM)
{
FS_FileSet flist;
FS.file_list(flist, initial, FS_ListFiles, "*.thm");
string_path fname;
FS.update_path(fname, initial, "textures.ltx");

if (!FS.exist(fname))
return;

#ifndef MASTER_GOLD
Msg("%s, count of .thm files: %d", __FUNCTION__, flist.size());
Msg("Processing textures.ltx in [%s]", initial);
#endif
DECLARE_DESCR_LOCK;

CInifile ini(fname);

#ifndef SINGLETHREADED_TEXTURES_DESCR
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
const auto processAssociation = [&](const CInifile::Item& item)
{
if (listTHM)
Msg("\t\t%s = %s", item.first.c_str(), item.second.c_str());

LOCK_DESCR();
texture_desc& desc = m_texture_details[item.first];
cl_dt_scaler*& dts = m_detail_scalers[item.first];
desc.m_assoc = new texture_assoc();

This comment has been minimized.

Copy link
@FreeZoneMods

FreeZoneMods May 11, 2019

Contributor

Maybe add R_ASSERT(desc.m_assoc == nullptr)? And could the same item be processed in more than 1 thread in the same time (in case of parallel mode)?

This comment has been minimized.

Copy link
@Xottab-DUTY

Xottab-DUTY May 12, 2019

Author Member

Ok, I've fixed memory leak with 10dea12, but situation, when the same item would be processed by more than 1 thread at the same time is still not handled properly. This is a potential crash.

UNLOCK_DESCR();

string_path T;
float s;

int res = sscanf(*item.second, "%[^,],%f", T, &s);
R_ASSERT(res == 2);
desc.m_assoc->detail_name = T;
dts = new cl_dt_scaler(s);
desc.m_assoc->usage = 0;
if (strstr(item.second.c_str(), "usage[diffuse_or_bump]"))
desc.m_assoc->usage = (1 << 0) | (1 << 1);
else if (strstr(item.second.c_str(), "usage[bump]"))
desc.m_assoc->usage = (1 << 1);
else if (strstr(item.second.c_str(), "usage[diffuse]"))
desc.m_assoc->usage = (1 << 0);
};
PROCESS_DESCR(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
const auto processSpecification = [&](const CInifile::Item& item)
{
if (listTHM)
Msg("\t\t%s = %s", item.first.c_str(), item.second.c_str());

const bool listTHM = strstr(Core.Params, "-list_thm");
LOCK_DESCR();
texture_desc& desc = m_texture_details[item.first];
UNLOCK_DESCR();

desc.m_spec = new texture_spec();

string_path bmode;
const int res =
sscanf(item.second.c_str(), "bump_mode[%[^]]], material[%f]", bmode, &desc.m_spec->m_material);
R_ASSERT(res == 2);
if ((bmode[0] == 'u') && (bmode[1] == 's') && (bmode[2] == 'e') && (bmode[3] == ':'))
{
// bump-map specified
desc.m_spec->m_bump_name = bmode + 4;
}
};
PROCESS_DESCR(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

DECLARE_DESCR_LOCK;

const auto processFile = [&](const FS_File& it)
{
// Alundaio: Print list of *.thm to find bad .thms!
if (listTHM)
Log(it.name.c_str());
Log("\t", it.name.c_str());

string_path fn;
FS.update_path(fn, initial, it.name.c_str());
Expand All @@ -70,10 +158,10 @@ void CTextureDescrMngr::LoadTHM(LPCSTR initial)
if (STextureParams::ttImage == tp.type || STextureParams::ttTerrain == tp.type ||
STextureParams::ttNormalMap == tp.type)
{
LOCK_DESCR(lock);
LOCK_DESCR();
texture_desc& desc = m_texture_details[fn];
cl_dt_scaler*& dts = m_detail_scalers[fn];
UNLOCK_DESCR(lock);
UNLOCK_DESCR();

if (tp.detail_name.size() &&
tp.flags.is_any(STextureParams::flDiffuseDetail | STextureParams::flBumpDetail))
Expand Down Expand Up @@ -115,12 +203,7 @@ void CTextureDescrMngr::LoadTHM(LPCSTR initial)
}
};

#ifdef SINGLETHREADED_TEXTURES_DESCR
for (auto& it : flist)
processFile(it);
#else
tbb::parallel_for_each(flist, processFile);
#endif
PROCESS_DESCR(flist, processFile);
}

void CTextureDescrMngr::Load()
Expand All @@ -130,11 +213,16 @@ void CTextureDescrMngr::Load()
timer.Start();
#endif // #ifdef DEBUG

LoadTHM("$game_textures$");
LoadTHM("$level$");
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, .thm loading time: %d ms", __FUNCTION__, timer.GetElapsed_ms());
Msg("%s, texture descriptions loaded for %d ms", __FUNCTION__, timer.GetElapsed_ms());
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion src/Layers/xrRender/TextureDescrManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class CTextureDescrMngr
map_TD m_texture_details;
map_CS m_detail_scalers;

void LoadTHM(LPCSTR initial);
void LoadTHM(pcstr initial, bool listTHM);
void LoadLTX(pcstr initial, bool listTHM);

public:
~CTextureDescrMngr();
Expand Down

0 comments on commit 50a4548

Please sign in to comment.