Jump to content

  • Log In with Google      Sign In   
  • Create Account


Resource referencing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 solenoidz   Members   -  Reputation: 514

Like
0Likes
Like

Posted 25 October 2012 - 01:39 AM

Resource management
Hello people.
I have some doubts about how to handle correctly certain situation.
Lets say I have a class CMesh that looks like so :
[source lang="cpp"]class CMesh { Matrix m_matrix ; VertexData* m_vertexData ;string fileName ;};[/source]



Also, I have a list where I store created meshes.
[source lang="cpp"]std::vector<CMesh*> g_pMeshList ;[/source]
When I need a new mesh, I load it from file only if it isn't alrady loaded. I do a simple search
in the g_pMeshList and if there is already a mesh with the same file name, I skip loading the vertices
twice, but store a pointer to the found mesh and increament it's reference counter.
Problem comes, when I need to render the meshes, because in the CMesh class, some of the data belongs
to the particular mesh instance, for example the matrix is unique for every mesh instance, but some of the
data like m_vertexData is shared among many mesh instances to avoid duplicating the vertex data and other
resources like textires, shaders etc.
Question is, how to approach correctly such a problem. Do I need to put a variable in the mesh class that
would indicate if it's a complete mesh or if it's a reference mesh that do not own resources it needs to render,
but points to other mesh resources ?
What if there is a lot more resources than vertex Data, for example lots of textures etc.Do I have to manually point them to already loaded mesh
resources ? Should I encapsulate mesh resources that could be shared among mesh instances in a separate class and store
a pointer to that class, in order to make it easier to reference them, as opposed to manually point every vertex buffer, texture, shader etc.
Thank you in advance.

Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 6691

Like
2Likes
Like

Posted 25 October 2012 - 01:57 AM

You need to wrap you mesh in an other class, i.e. model, something like this:

class CAnimationData {}
class CMesh {}

class CModel {
  CMesh m_mesh;
  CAnimationData m_aniData;
  Matrix m_matrix;
  int m_currentAnimationFrame;
  Color m_color;
...
}
For each ingame instance of a model you need to create a CModel object, but mulitple CModels can sharea a single mesh, animation data etc.

Edited by Ashaman73, 25 October 2012 - 01:58 AM.


#3 noizex   Members   -  Reputation: 842

Like
0Likes
Like

Posted 25 October 2012 - 03:49 AM

Hello,

Seeing this topic, I thought I will ask for some help with another problem related to resource sharing. Let me type some very pseudo code here:

class Model
{
	std::vector<Animation>* GetAnimations() { return &m_Animations; }

	VertexArrayObject	   m_VAO;
	std::vector<Animation>   m_Animations;
	Joints				  m_Joints
}
class ModelInstance
{
	ModelInstance(Model* model): m_Model(model)
	{
	   m_Animations = m_Model->GetAnimations();  // returns pointer to std::vector<Animations>
	}
  
	StartAnimation()
	{
	   m_CurrentAnimation = &m_Animations[0];  // starts first animation in animation vector and keeps its pointer in m_CurrentAnimation
	}

	Model*	  m_Model;
	std::vector<Animation>*  m_Animations;
	Animation*  m_CurrentAnimation;
}

Now Model is kept in AssetRepository class which always has just one instance of a given model file. It returns pointer (or shared_ptr) to a given asset when its requested by asset.Get("model.file").

The problem is when you want to reload the Model and have the effects applied to all ModelInstances using that model. As you can see from that pseudo code, I often reference data from Model (which are on stack) by pointer, and as long as the Model stays in place (and it will, plus I will use shared_ptr for resource sharing) its okay. But lets say I do something like this:

model.Reload("mymodel");

that reinitializes data in the instance - there is a chance that vector will be resized or something else will happen that invalidates pointers that are referenced by ModelInstance. For example, m_CurrentAnimation will point to some garbage now, if it happens that animation vector changes its place in memory due to resize and other actions.

Question is - how to reference Model data in a way that keeps them consistent - because copying them is not that good idea, thats why we have one copy of Model. Referencing them by pointers works as long as Model is not reloaded or modified in any way that changes memory addresses of data thats referenced outside.

I can see few solutions here
  • Keep pointer-approach, but ensure that when model reloads, these pointers are invalidated and new ones are assigned - this is tricky as ModelInstance can be in the middle of some animation when the reload occurs and we'll be modyfing pointers that may be used at the moment?
  • Don't reference Model data by pointers at all, always retrieve fresh data by some Get() methods - but what if we need to pass some animation data further - we either have to copy it (bad) or use pointer (same problem as above when some reload occurs and the pointer is still used somewhere)
  • Don't initialize data on stack, so it wont break in std::vector when referenced by pointer - so all animations etc. are initialized by 'new'. This will prevent breaking pointers, but the pointer may still point to a wrong data after reload (if, for example one less animation is loaded on next reload)
I'd really like to be able to reload Model in a way that leaves state of the program intact.

Additional question - lets say my AssetRepository uses shared_ptr for referencing its resources. All is fine, resources are referenced by various objects in game and shared_ptr guards the destrucion of object only when nothing references it - but what if I want to control the process a bit more. For example, if resource is no longer referenced by any in-game object, I don't delete it immediately, but I free GPU buffers and textures, but keep them on CPU for a while longer, and then remove it after nothing references this model again. How can I do this with shared_ptr, can I be somehow notified that pointer reached resource count 1 so AssetRepository is only one thats referencing it, so it means we clean the GPU but keep it on CPU side for a while, and if nothing happens in some specified time we remove it completly from memory?

#4 solenoidz   Members   -  Reputation: 514

Like
0Likes
Like

Posted 25 October 2012 - 05:16 AM

You need to wrap you mesh in an other class, i.e. model, something like this:


Thank you. So something like my last paragraph in the first post would be sufficient.
Btw, where the "like" buttons disappeared from this forum ?

#5 Ashaman73   Crossbones+   -  Reputation: 6691

Like
0Likes
Like

Posted 25 October 2012 - 06:28 AM

Thank you. So something like my last paragraph in the first post would be sufficient.

Yes. Think first of identifying shared data and group them logically. Something like this:

SkeletonData
=> bone hierachy

TextureData
=> a single texture

AnimationData
=> SkeletonData
=> animation frames

Mesh
=> optional SkeletonData
=> skin data (vertex/bone weights)
=> vertex data
=> texture data

Model
=> Mesh
=> AnimationData
=> current animation state
=> some flags (ie. visible)

Entity
=> Model
=> world position
=> physics data
=> game logic data


Shared data should be identified by a unique id, a filename is a good idea, at least take a speaking name instead of some generated number :-)
It is advisable to write a resource class which handles shared data, your list is the right start. Basically something like this:

// pseudocode
class CResource {
String m_id;

}
class CMesh : CResource {...}

class CResourceManager<MyRes>
{
   map<String, MyRes*> m_resourceList;

 MyRes* accessResource(String id) {
    MyRes* result = m_resourceList.get(id);
    if(result==null) {
	  result = new MyRes(); 
	  result.init(id);
	  m_resourceList.put(id, result);
	 }
	 return result;
	 }

    void cleanup() {
	  // check if there are resources no longer needed (aka garbage collection)
   }
}


#6 solenoidz   Members   -  Reputation: 514

Like
0Likes
Like

Posted 25 October 2012 - 06:49 AM

Thanks.
But what is MyRes, should it be CResource ?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS