Now that I have a working cache and resource manager how do I go about finding the right size cache for the game?
Right now it's set at 4mb which is ridiculously low and gets into a cache thrash rather quickly.
Luckily when the cache miss ratio is above 60% meaning 6 out of 10 cache requests miss the cache will raise the max size to accomodate the situation.
It's a little weird not having texture members in my objects and my code now looks like this:
Code:
void CSkySphere::Render(void)
{
//Request texture from resource cache
CTexture *pTexture=(CTexture *)X3DGetResMgr()->STLGet(m_strTexture);
//If we didn't get it we must re-create the resource
if (!pTexture)
{
pTexture=new CTexture();
pTexture->CreateFromFile(m_strTexture);
}
//Set the texture
m_spDevice->SetTexture(0,pTexture->GetTexture());
...
...
Code:
HRESULT CTexture::CreateFromFile(std::string strFile)
{
//::MessageBox(0,"Creating texture resource",0,0);
//Call base and load the data from the file into the buffer
bool bFailed=Create(strFile);
HRESULT hr=0;
if (!bFailed)
{
//Create texture interface from buffer data
hr=D3DXCreateTextureFromFileInMemory(X3DGetDevice(),
(LPCVOID)m_pBuffer,
m_uFilesize,
&m_pTexture);
//If texture creation fails it is a major error
if (FAILED(hr)) ::MessageBox(0,"Failed to create texture from memory",0,0);
//If all went well then add us to the resource cache and tree
if (hr==D3D_OK) X3DGetResMgr()->STLAdd(this);
return hr;
}
else
{
//Major error here
::MessageBox(0,"Cannot create texture resource",0,0);
return hr;
}
}
Code:
bool CResMgr::STLAdd(CResource *pObj)
{
//Name
std::string strTemp;
//iterator for find
std::map<std::string,STLListNode *>::iterator it;
//If top is NULL the list is empty and therefore the tree is too
if (m_pTop==NULL)
{
//Create new node and place at top of cache
STLListNode *pNewNode=new STLListNode();
pNewNode->pPrev=NULL;
pNewNode->pNext=NULL;
pNewNode->pResource=pObj;
m_pTop=pNewNode;
m_pBottom=pNewNode;
//Insert node into map
strTemp=pNewNode->pResource->GetName();
it=m_mapRes.find(strTemp);
if (it==m_mapRes.end())
{
m_mapRes.insert(std::make_pair(strTemp,pNewNode));
m_dwCurSize+=sizeof(*pNewNode)+pNewNode->pResource->GetSize();
} else return true;
}
else
{
//Create new node and place at top of cache
STLListNode *pNewNode=new STLListNode();
pNewNode->pPrev=NULL;
pNewNode->pNext=m_pTop;
pNewNode->pResource=pObj;
strTemp=pNewNode->pResource->GetName();
it=m_mapRes.find(strTemp);
if (it==m_mapRes.end())
{
//Set bottom if node is added and only 1 node in list
if (m_pTop->pNext==NULL) m_pBottom=m_pTop;
m_pTop=pNewNode;
//Insert node into map
m_mapRes.insert(std::make_pair(strTemp,pNewNode));
m_dwCurSize+=sizeof(*pNewNode)+pNewNode->pResource->GetSize();
} else return true;
}
return false;
}
I'm glad I finally have this working because it has been a major pain in the arse.
Here is a screenshot after about 15 seconds of running the game. The only thing using the cache here is the space texture you see in the background. As more and more objects start using the cache the ratios will change rather quickly. It will take a bit of time to convert the existing object classes over to the cache system but when I get that done I will show some more. I've got some models, sounds, music, planets, and other items ready to place in this sector.