diff --git a/Jusin154_Team3D/Engine/Private/Resource_Manager.cpp b/Jusin154_Team3D/Engine/Private/Resource_Manager.cpp index 29a6760f8..0a8f5f160 100644 --- a/Jusin154_Team3D/Engine/Private/Resource_Manager.cpp +++ b/Jusin154_Team3D/Engine/Private/Resource_Manager.cpp @@ -4,27 +4,55 @@ CResource_Manager::CResource_Manager(ID3D11Device* pDevice) :m_pDevice{ pDevice } { - SAFE_ADDREF(pDevice); + SAFE_ADDREF(m_pDevice); } ID3D11ShaderResourceView* CResource_Manager::Add_Texture(const _char* pFilePath) { - lock_guard lock(m_mtxTexture); + if (pFilePath == nullptr || pFilePath[0] == '\0'){ + return nullptr; + } + filesystem::path pathFile = pFilePath; + const _wstring wstrKeyOriginal = pathFile.filename().wstring(); + + { // 브레이스 해제 금지 + shared_lock sharedLock(m_smtxTexture); - ID3D11ShaderResourceView* pSRV = Find_SRV(pFilePath); - - if (nullptr == pSRV) - { - pSRV = Load_SRV(pFilePath); - - if (nullptr == pSRV) + auto iter = m_Resources.find(wstrKeyOriginal); + if (iter != m_Resources.end()) { - return nullptr; + SAFE_ADDREF(iter->second); + return iter->second; } + // sharedLock 소멸 + } // 브레이스 해제 금지 + + ID3D11ShaderResourceView* pSRV = Load_SRV(pathFile); + + if (pSRV == nullptr){ + return nullptr; } - SAFE_ADDREF(pSRV); + { // 브레이스 해제 금지 + unique_lock uniqueLock(m_smtxTexture); + auto iter = m_Resources.find(wstrKeyOriginal); + if (iter != m_Resources.end()) + { + SAFE_RELEASE(pSRV); // 여기까지 왔는데 end가 아니다? -> 다른 스레드가 넣음 -> 이 srv는 이미 로드됨. -> 삭제해야 됨 + + SAFE_ADDREF(iter->second); + return iter->second; // sharedLock 소멸 + } + + m_Resources.emplace(wstrKeyOriginal, pSRV); + // sharedLock 소멸 + } // 브레이스 해제 금지 + +#ifdef _DEBUG + ++m_iCount; +#endif // _DEBUG + SAFE_ADDREF(pSRV); return pSRV; } @@ -35,62 +63,37 @@ HRESULT CResource_Manager::Initialize(_uint iResourceCount) return S_OK; } -ID3D11ShaderResourceView* CResource_Manager::Find_SRV(const _char* pFilePath) -{ - _char szFileName[MAX_PATH] = {}; - _splitpath_s(pFilePath, nullptr, 0, nullptr, 0, szFileName, MAX_PATH, nullptr, 0); - - auto iter = m_Resources.find(CMyTools::ToWstring(szFileName)); - - if (iter == m_Resources.end()) - return nullptr; - - return iter->second; -} - -ID3D11ShaderResourceView* CResource_Manager::Load_SRV(const _char* pFilePath) +ID3D11ShaderResourceView* CResource_Manager::Load_SRV(const filesystem::path& pathFile) { ID3D11ShaderResourceView* pSRV = nullptr; - _char szTextureFileName[MAX_PATH] = {}; - _char szTextureFilePath[MAX_PATH] = {}; - _char szDrive[MAX_PATH] = {}; - _char szDir[MAX_PATH] = {}; - _char szFileName[MAX_PATH] = {}; - _char szEXT[MAX_PATH] = {}; - _splitpath_s(pFilePath, szDrive, MAX_PATH, szDir, MAX_PATH, szFileName, MAX_PATH, szEXT, MAX_PATH); - strcpy_s(szTextureFileName, szDrive); - strcat_s(szTextureFileName, szDir); - strcat_s(szTextureFileName, szFileName); + const filesystem::path pathExtensionOriginal = pathFile.extension(); - strcat_s(szTextureFilePath, szTextureFileName); - strcat_s(szTextureFilePath, ".dds"); + filesystem::path pathDDSFile = pathFile; + pathDDSFile.replace_extension(L".dds"); - HRESULT hr = {}; + HRESULT hr = CreateDDSTextureFromFile( + m_pDevice, + pathDDSFile.wstring().c_str(), + nullptr, + &pSRV + ); - hr = CreateDDSTextureFromFile(m_pDevice, CMyTools::ToWstring(szTextureFilePath).c_str(), nullptr, &pSRV); // 일단 dds로 시도하고 if (FAILED(hr)) { - memset(szTextureFilePath, 0, sizeof(_char) * MAX_PATH); // 실패하면 원래 확장자로 다시 시도 - strcat_s(szTextureFilePath, szTextureFileName); - strcat_s(szTextureFilePath, szEXT); + filesystem::path pathFileOriginal = pathFile; + pathFileOriginal.replace_extension(pathExtensionOriginal); - if (false == strcmp(".tga", szEXT)) { - assert(false); // trying to Load TGA - hr = S_OK; - } - else { - hr = CreateWICTextureFromFile(m_pDevice, CMyTools::ToWstring(szTextureFilePath).c_str(), nullptr, &pSRV); + hr = CreateWICTextureFromFile( + m_pDevice, + pathFileOriginal.wstring().c_str(), + nullptr, + &pSRV + ); + + if (FAILED(hr)){ + return nullptr; } } - - if (E_FAIL == hr) - return nullptr; - - m_Resources.emplace(CMyTools::ToWstring(szFileName), pSRV); -#ifdef _DEBUG - ++m_iCount; -#endif // _DEBUG - return pSRV; } diff --git a/Jusin154_Team3D/Engine/Private/Texture.cpp b/Jusin154_Team3D/Engine/Private/Texture.cpp index 611e7278c..d30efd096 100644 --- a/Jusin154_Team3D/Engine/Private/Texture.cpp +++ b/Jusin154_Team3D/Engine/Private/Texture.cpp @@ -97,8 +97,9 @@ HRESULT CTexture::Load_SRV(const _char* szPath, ID3D11ShaderResourceView** ppSRV { ID3D11ShaderResourceView* pSRV = m_pGameInstance->Add_Resource(szPath); - if (nullptr == pSRV) + if (nullptr == pSRV){ return E_FAIL; + } *ppSRV = pSRV; diff --git a/Jusin154_Team3D/Engine/Private/VIBuffer_Model_Instance.cpp b/Jusin154_Team3D/Engine/Private/VIBuffer_Model_Instance.cpp index d267d8180..21575c4a5 100644 --- a/Jusin154_Team3D/Engine/Private/VIBuffer_Model_Instance.cpp +++ b/Jusin154_Team3D/Engine/Private/VIBuffer_Model_Instance.cpp @@ -272,8 +272,9 @@ HRESULT CVIBuffer_Model_Instance::Ready_Materials() pSRV = m_pGameInstance->Add_Resource(m_MeshMaterialPathes[i][j].c_str()); - if (nullptr == pSRV) + if (nullptr == pSRV){ return E_FAIL; + } SRVs.push_back(pSRV); } diff --git a/Jusin154_Team3D/Engine/Public/Resource_Manager.h b/Jusin154_Team3D/Engine/Public/Resource_Manager.h index 3b27f4892..837f81915 100644 --- a/Jusin154_Team3D/Engine/Public/Resource_Manager.h +++ b/Jusin154_Team3D/Engine/Public/Resource_Manager.h @@ -9,15 +9,14 @@ class CResource_Manager final : public CBase private: CResource_Manager(ID3D11Device* pDevice); virtual ~CResource_Manager() = default; - mutex m_mtxTexture = {}; + shared_mutex m_smtxTexture = {}; public: ID3D11ShaderResourceView* Add_Texture(const _char* pFilePath); private: HRESULT Initialize(_uint iResourceCount); - ID3D11ShaderResourceView* Find_SRV(const _char* pFilePath); - ID3D11ShaderResourceView* Load_SRV(const _char* pFilePath); + ID3D11ShaderResourceView* Load_SRV(const filesystem::path& pathFile); private: #ifdef _DEBUG diff --git a/Jusin154_Team3D/Engine/Public/pch.h b/Jusin154_Team3D/Engine/Public/pch.h index c27ab5229..c226fdfe9 100644 --- a/Jusin154_Team3D/Engine/Public/pch.h +++ b/Jusin154_Team3D/Engine/Public/pch.h @@ -58,6 +58,7 @@ using namespace DirectX; #include #include #include +#include using namespace std;