Sign in to follow this  
Conny14156

return std::map Question[Solved]!

Recommended Posts

Conny14156    279

Hi,

I just notice something very wierd today, when I try to return the std::map<string,ID3DResourceView*> value with help from map[std::string] I get the ID3D11ResourceView* pointer, but that only works when I return it two example I have in my code 

//While this work
return TotalTexture[textureName];


/*
This wont, Both is identical std::map<string,ID3D11ShaderResourceView*>, but the diffrent
is that this code down here insert the string instead with a null ID3D11ShaderResourceViewpointer. And I know its that line cause 
*/
ID3D11ShaderResourceView* pt;
pt = Model->Texture.Diffuse.Texture[Model->lastMaterial];	
Gfx->D3d11DevCon->PSSetShaderResources(0,1,&pt);//<---- Cause the next line here the map 
//had changed with one extra material with a null pointer. 

 

 

Share this post


Link to post
Share on other sites
noizex    1437

Sorry, could you rephrase your problem, because honestly I can't seem to understand what is it about. :) Attached code doesn't make any sense too, try to put here some meaningful testcase.

Share this post


Link to post
Share on other sites
Hodgman    51226

operator[] is a read + write operation. If the specified key doesn't exist, then it is inserted into the map.

If you want to look up an item that might be in the map, without adding it in cases where it doesn't exist, then use find instead of [].

Share this post


Link to post
Share on other sites
Conny14156    279

Oh, Sorry It was late at night >.< (Around 4 in the morning XD) Let me try it again.

 

What am trying to do is to avoid loading more than one of each texture file, so I made a std::map wtih the texture name as the key and the value would be a ID3D11ShaderResourceView* that contain the texture.

 

Each time I "Load" a model with materials it would check if the material would already exist inside the map, to avoid loading multipile  time of the same texture.

if the texture already had been loaded i would get it by calling RequestTexture which looks like this


ID3D11ShaderResourceView* GfxHandler::RequestTexture(std::string textureName)
{
    if(TotalTexture.find(textureName) != TotalTexture.end())
    {
        MessageBox(0,"Request Granted",textureName.c_str(),MB_OK);
        TotalTextureCounter[textureName]++;
        return TotalTexture[textureName];
    }
    else
    {
        MessageBox(0,"Request Failed",textureName.c_str(),MB_OK);
    }    
    return false;
}

 

Where the TotalTexture and TotalTexture looks like

std::map<std::string,ID3D11ShaderResourceView*> TotalTexture;
std::map<std::string,int> TotalTextureCounter;

 

The ReqeustTexture returns a shader resource which I assign and insert into a seperate class which I would call the "Model", the model class have its own map which looks like the same except the only texture that gets inserted there would be the materials that the model use, the reason why I try to do this is to avoid unnecessary looping inside the bigger map.

 

This is how I do it inside my LoadModel

Its works great, my Testing does indeed get the same adress as the loaded texture from the map


ID3D11ShaderResourceView* Testing = 0;
Gfx->LoadTextureOld(Model->Texture.Diffuse.Location,Model->Texture.Diffuse.TextureOld,Model->Texture.Diffuse.Sampler);
for(int i = 0; i < Model->Materials.size();i++)
{
	Model->Materials[i].materialName += ".jpg";
	if(Gfx->TotalTexture.find(Model->Materials[i].materialName) == Gfx->TotalTexture.end())
		{
	
		Gfx->LoadTexture(Model->Materials[i].materialName,Testing);
			Model->Texture.Diffuse.Texture.insert(Model->Texture.Diffuse.Texture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Model->Texture.Diffuse.TextureOld));
			Gfx->RequestInsertionOfTexture(Model->Materials[i].materialName,Testing);
	//		Gfx->TotalTexture.insert(Gfx->TotalTexture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Testing));
			
		}
		else
		{
			Testing = Gfx->RequestTexture(Model->Materials[i].materialName);	
	 		Model->Texture.Diffuse.Texture.insert(Model->Texture.Diffuse.Texture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Model->Texture.Diffuse.TextureOld));
			//Gfx->RequestInsertionOfTexture(Model->Materials[i].materialName,Testing);
	//		Gfx->TotalTexture.insert(Gfx->TotalTexture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Testing));
			
		}
	}
 

 

Now when Am trying to render my object, I try to change material depending on what group it is.

So I did something like this


for(int i = 0; i < Model->Total.Group; i++)
	{
		if(Model->lastMaterial != Model->groupMaterial[i])
		{
                  //First method
                  Model->lastMaterial = Model->groupMaterial[i];
                  Gfx->D3d11DevCon->PSSetShaderResources(0,1,& Model->Texture.Diffuse.Texture[Model->lastMaterial]);//<---- //Texture
// */

//Second method Model->lastMaterial = Model->groupMaterial[i]; pt = Model->Texture.Diffuse.Texture[Model->lastMaterial]; Gfx->D3d11DevCon->PSSetShaderResources(0,1,&pt);//<---- Texture


         //*/

} Gfx->D3d11DevCon->DrawIndexed ( Model->DrawingIndexIndices.IndexCount[i], Model->DrawingIndexIndices.StartIndex[i], 0 ); }

 

In which case instead of getting the texture it somehow decided to insert the texture 

like this

Share this post


Link to post
Share on other sites
Conny14156    279

operator[] is a read + write operation. If the specified key doesn't exist, then it is inserted into the map.

If you want to look up an item that might be in the map, without adding it in cases where it doesn't exist, then use find instead of [].

oh, >.<

Took me so long to write my last post without noticing a new reply thanks, will check it asap to see if that is the case!

Share this post


Link to post
Share on other sites
noizex    1437

Woah man, from completly unclear you went into totally detailed :) Thanks, it didn't have to be SO DETAILED, but props for this. 

 

Seems like Hodgman was able to figure out the problem from that first vague description, and its indeed what happens. Try to stay away from [] operator as its convenient but it will insert element into map if it can't find it. Its probably better to use find() and insert() / emplace() as they have single behavior (unless of course inserting new element if it couldnt find one is what you really need).

Share this post


Link to post
Share on other sites
Conny14156    279

is there any diffrent in performance effiency between [] and find()? seeing how my map will get pretty big and gets alot of request in short intervalls. 

if there is I have the time to try atleast to make it so that this problem wont happend again >.<

Share this post


Link to post
Share on other sites
SiCrane    11839
map::find() will generally perform a little better than using operator[] because it's semantics are simpler.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this