Should evey object have it's own LPDIRECT3DDEVICE9?

Started by
14 comments, last by D3DXVECTOR3 19 years, 11 months ago
Or pass the LPDIRECT3DDEVICE9 as an parameter when needed?? Lets say I have a class that handles DirectX *.x meshes, its need a LPDIRECT3DDEVICE9 for loading and rendering. Should classes like this one have his own LPDIRECT3DDEVICE9 or use the LPDIRECT3DDEVICE9 from the main class ??


class DXMesh
{
public:
	DXMesh();
	~DXMesh();

	HRESULT LoadMesh(LPDIRECT3DDEVICE9 pd3dDevice);
	void RenderMesh();

private:

	LPDIRECT3DDEVICE9	gd3dDevice;
	LPD3DXMESH              gMesh;
	D3DMATERIAL9*           gMeshMaterials;
	LPDIRECT3DTEXTURE9*     gMeshTextures;
	DWORD                   gdwNumMaterials;
};

In LoadMesh I have a line like this : gd3dDevice = pd3dDevice; or Like this


class DXMesh
{
public:
	DXMesh();
	~DXMesh();

	HRESULT LoadMesh(LPDIRECT3DDEVICE9 pd3dDevice);
	void RenderMesh(LPDIRECT3DDEVICE9 pd3dDevice);

private:

	LPD3DXMESH              gMesh;
	D3DMATERIAL9*           gMeshMaterials;
	LPDIRECT3DTEXTURE9*     gMeshTextures;
	DWORD                   gdwNumMaterials;
};

I''m asking this because of memory usage. Which is the best lets say in memory usage and speed?? I have always used the 2nd option. pass the LPDIRECT3DDEVICE9 as a parameter when needed, but now I have changed it (along with some other changes) and my memory usuage went from 50MB to about a 100MB and this is the only reason that comes to my mind.
Advertisement
I think it very unlikely that this is causing the change in memory. I make the device global and use extern to use it in other source files. I know globals are bad but so is passing a pointer around all over the shop and making a local copy is even worse! So I would say go with it as global but try to find a way to prevent the problems with globals - multiple instances, who creates them ? who deletes them? etc. e.g. you could create a singleton class whose function is to manage globals like this
------------------------See my games programming site at: www.toymaker.info
Passing pointers used to eat a lot, but those days are gone now so you can savely store one single device pointer in a parent class, or you can always keep it global(Or namespaced) ofcourse

But creating overhead for all your classes, that''s not the way to go, not to mention the mess it creates.

WHat i did, don''t knowwhether its good or bad, was to havea pointer to a device in every object and have teh creation of the device in one class.

regards

ace
If you have a case where code that knows nothing about D3D calls a member function that needs a device pointer, you <u>can''t</u> pass it as a parameter because it isn''t available. You might save yourself some trouble if you plan ahead for this and keep a copy of the pointer in the class.
I think you should be using only one IDirect3DDevice9 interface
each mesh should not have its own d3d device
Having the pointer (which doesn''t use much memory (32 bits I think?)) in every class is a nice way to have quick access to it within the class''s private functions and it prevents having to pass the pointer (except in the constructor). I like to do it this way for that reason. On the other hand, if 100 MB of memory usage (you are talking about system memory right?) is too much for the requirements you want to meet, then pass it through functions...at the expense of a little larger executable. You may be able to optimize things a little more too. For instance, instead of putting the pointer in every single class, put it in the parent class (like a manager of some type), then from the child classes, call Parent->Device, where you saved the Parent of the class.

class cWindow
{
public:
LPDIRECT3DDEVICE9 pd3dDevice;
cWindow(LPDIRECT3DDEVICE9 fp3dDevice);
};

class cWindowComponent
{
private:
pfUpdateVertices(void);
cWindow *pParent;
public:
cWindowComponent(cWindow *fpParent);
};

cWindowComponent:fUpdateVertices(void)
{
// Do whatever with pParent->p3dDevice.
}

This way, you could make 1 window with 20 components, but only store the device in the 1 window. You could take it a step further by storing the device in the cWindowContainer class, then calling pParent->Parent(this one would have to be public)->p3dDevice; for all windows and all components in every window. It would generate a slightly small loss of cpu (are we counting nanoseconds? Yes we are...we make games. ) but it would reduce your memory overhead.

Chris
Chris ByersMicrosoft DirectX MVP - 2005
It''s just a 4 byte pointer, so passing a copy in the creator and storing it locally isn''t that big a deal.
Alternatively you can make your own (singleton) device class with the pd3ddevice as a public static member and use that in every draw call.
Or you could have a static member in DXMesh holding the LPDIRECT3DDEVICE9. Then you would just set it in your D3D initiation code like so: DXMesh::device = pointerToDevice. Now you have one pointer for all meshes.
quote:Original post by The Beholder
Or you could have a static member in DXMesh holding the LPDIRECT3DDEVICE9. Then you would just set it in your D3D initiation code like so: DXMesh::device = pointerToDevice. Now you have one pointer for all meshes.


That feels wrong, if the device manipulates static data in the mesh class or calls a static function. Then everytime you add another class (a sprite class, progressive mesh class) you have to mofidy code in your device init function.

This topic is closed to new replies.

Advertisement