ID3D10ShaderResourceView* and std::map

Started by
7 comments, last by blackslayer 12 years, 8 months ago
Hi. I've madea simple Texture Manager for DrectX 10 using ID3D10ShaderResourceView and
std::map.I just want it be simple for my small demos. Here is how I organazied it:

class TextureManager
{
private:
static class TextureManager *m_TextureManager;
map<wstring, ID3D10ShaderResourceView*, less<wstring> > textures;
map<wstring, ID3D10ShaderResourceView*, less<wstring> >::iterator iter;
ID3D10Device* md3dDevice;
public:
TextureManager (void);
~TextureManager (void);
static TextureManager *GetManager();
bool addTexture(wstring path);
void deleteTexture(wstring path);
void clearTextures();
void setDevice(ID3D10Device* dev);
void checkDevice();
ID3D10ShaderResourceView* GetTextureView(wstring path);
ID3D10Device* getDevice();
ID3D10ShaderResourceView* TexView;
};

[color="#000000"]it's a singleton class, [color="#000000"]addTexture and GetTextureView implemented like this:
bool TextureManager::addTexture(wstring path)
{
checkDevice();
iter=textures.find(path.c_str());
if(iter==textures.end())
{
HRESULT hr=D3DX10CreateShaderResourceViewFromFile(md3dDevice, path.c_str(), NULL, NULL, &TexView, NULL);
if(hr!=S_OK)
return false;
textures.insert( pair<wstring,ID3D10ShaderResourceView*>(path.c_str(),TexView));
return true;
}

return false;
}

ID3D10ShaderResourceView* TextureManager::GetTextureView(wstring path)
{
checkDevice();
iter=textures.find(path.c_str());
if(iter==textures.end())
if(!addTexture(path.c_str()))
return NULL;
return textures[path.c_str()];
}






[color="#000000"]The problem is when I'm using my manager's function GetTextureView to set new texture into shader like this:

[color="#000000"]mfxVar->SetResource(txr_man->GetTextureView(filenames[texIndex].c_str()));

[color="#000000"]I got my application 200-300 FPS down :[color="#000000"]( If I use just an [color="#000000"]ID3D10ShaderResourceView*[color="#000000"]array in application,instead of texture manager FPS looks good. Can't understand why putting [color="#000000"]ID3D10ShaderResourceView [color="#000000"]into <map>decreases FPS. Sure I can get rid of <map> just using an array of [color="#000000"]ID3D10ShaderResourceView [color="#000000"]in my manager while keeping it's functionality, but then I can't resize my texture array. Someone have any ideas about?
Advertisement
200-300 FPS down?? How much FPS do you have, 2k?

Never measure time in fps, always use 1/fps to get the actual time of the frame.

200-300 FPS down?? How much FPS do you have, 2k?

Never measure time in fps, always use 1/fps to get the actual time of the frame.





While in release build it's about 3000 but it's because there isn't much things in scene: only textured level, and camera for now. But the question is about why storing ID3D19ShaderResourceView* in std::map slows down the game? Not about how large my FPS or how do I measure it.



[quote name='n3Xus' timestamp='1311500894' post='4839534']
200-300 FPS down?? How much FPS do you have, 2k?

Never measure time in fps, always use 1/fps to get the actual time of the frame.


While in release build it's about 3000 but it's because there isn't much things in scene: only textured level, and camera for now. But the question is about why storing ID3D19ShaderResourceView* in std::map slows down the game? Not about how large my FPS or how do I measure it.
[/quote]

It slows down the game because more instructions are executed, it's as simple as that. Not that those 300 fps really matter: 3000 fps is 0.33 ms PER frame. 2700 fps is 0.37 ms PER frame, thus your code takes 0.04 ms longer which is no big deal.


However, why do your methods take wstring by value? That always copies the string that is passed in. Even worse is that you are actually passing a const wchar_t* in to the methods, creating another copy while at it. If you have a wstring already, simply pass it to the method, you don't need and should not call .c_str()! You should also change the signature of your methods from method(wstring) to method(const wstring&), which makes them take a reference to a wstring (and thus no copying is involved).






bool TextureManager::addTexture(const wstring& path)
{
checkDevice();
iter=textures.find(path);
if(iter==textures.end())
{
HRESULT hr=D3DX10CreateShaderResourceViewFromFile(md3dDevice, path.c_str(), NULL, NULL, &TexView, NULL);
if(hr!=S_OK)
return false;
textures.insert( make_pair(path,TexView));
return true;
}

return false;
}

Sounds like a good idea, I'll try to implement like you said. Thanks man:)
Well tried written above. FPS still falls, maybe I need something else.

Well tried written above. FPS still falls, maybe I need something else.


What I've been trying to say is that you don't need to worry about that right now. Your game runs in 0.37 ms a frame. You have got another 59 ms to use before you should worry.
What we're trying to tell you is that FPS are NOT linear: 120 fps does NOT mean "twice as fast" as 60 fps. On the other hand, 0.4ms per frame is twice as fast as 0.8ms per frame.
The lower the FPS, the slower they get even lower under more work.

This explains why there seems to be a "huge" difference just by doing something as simple as a map insert/read, while it is actually neglectable.
Ok, I'll keep moving with my map then. As for now it will be enough to me having this performance. Thanks.

This topic is closed to new replies.

Advertisement