Sign in to follow this  
RVOswald

Pointer to D3D Device

Recommended Posts

RVOswald    100
Hello all..

I have recently begun programming in C++ With the DirectX libraries. The examples I have been following are working well for me, but they were all coded in one main cpp file, so I've started moving them out into classes.

Specifically I am working on a particle & particle emitter class, but I have a question about passing around the LPDIRECT3DDEVICE9. This is a pointer to the device and I declare one in my main cpp file

LPDIRECT3DDEVICE9 d3dDevice;


In my Particle class that I have written, I have to at points use the device. For example, I must load the texture
D3DXCreateTextureFromFile(device,textureFileName,&texture);


I digress... I have created a second LPDIRECT3DDEVICE9 in the scope of my Particle class. Then in the constructor I do this:
Particle(LPDIRECT3DDEVICE9* device, ...)
{
d3dDevice = device;
...
}


The method call being

Particle particle[PARTICLECOUNT];
for(int i = 0; i < PARTICLECOUNT; i++)
{
particle[i] = Particle(&d3dDevice, ...);
}

In effect I have a pointer to a pointer. Is this necessary or am I going a step too far with this? FYI it works with this method, but I am worried that I am creating unessential overhead?

Share this post


Link to post
Share on other sites
Aardvajk    13207
I personally find it more confusing to use the LP defines, especially for beginners getting their heads round pointers.

I prefer:


IDirect3DDevice9 *Device;


LPDIRECT3DDEVICE9 is just a typedef in d3dtypes.h:


typedef IDirect3DDevice9* LPDIRECT3DDEVICE9;


I think it has something to do with the bad old days of 16-bit addressing, when it was possible to have two different sized pointer types.

Share this post


Link to post
Share on other sites
RVOswald    100
Quote:
Original post by no such user
Why do each of your particles need to store a copy of the device pointer?


As I said, I need to use the device to create the texture for the particles, which is done in the constructor of the Particle class. It seems that anything over 10,000 particles causes the program to stop working, less than that and it takes a while to load but it does run smoothly.
I tried switching over to IDirect3DDevice9* but it's still just as slow. Is there another way to accomplish this, without each Particle having a device pointer?

Share this post


Link to post
Share on other sites
Aardvajk    13207
Sounds like you are rendering each particle one at a time. This will cripple your speed.

If any of your particles share a texture and render states, you really need to batch them into one vertex buffer and draw them in one go with one call to DrawPrimitive or whatever.

Share this post


Link to post
Share on other sites
unbird    8338
Quote:
As I said, I need to use the device to create the texture for the particles, which is done in the constructor of the Particle class


Even if every particle has a different texture, I'd rather store the texture than the device. If you really allocate that a lot of textures, I bet your "program stops working" issue is a run-out of video memory. Some debugging, checking the DX API result values or even enabling the DX debug runtimes should show you what it is exactly.

Another approach than storing the texture per particle is to draw groups of particles which share the same texture, if your rendering logic allows that. In that case the particle does not need to store the texture at all.

Edit: Ninja-ed by Aardvajk. Correct: The approach you are looking for is called batching.

Share this post


Link to post
Share on other sites
RVOswald    100
Okay, that makes sense. I am still learning here so my "logic" is hardly well defined. I'll look into ways of setting a single texture, it is apparent that I have a copy of the texture for each particle.

Thank you for the help.

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