Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


DirectX9 Depth Buffer to memory (fast)


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
10 replies to this topic

#1 ctMarine2009   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 July 2010 - 08:04 AM

Is there any way to write an a depth buffer to memory quickly? I'm using MRT. I lock the surface write it to memory and unlock it. and by that time its slowed my fps down to 3 or less. Any other way of doing this or do I need to wait until the developers move on to 10 or 11?

Thanks,
Chris

Sponsor:

#2 Evil Steve   Members   -  Reputation: 1987

Like
1Likes
Like

Posted 13 July 2010 - 08:35 AM

Why do you need the depth values in system memory?

The quickest way is probably to use a pixel shader to write the depth value to the render target and then to lock the render target - but that's still not going to be particularly quick. It might be a bit faster if you use two render targets in turn (double buffer them) and use D3DLOCK_DONOTWAIT when you lock the surface, but it'll introduce a frame of lag.

Essentially you're completely serialising the CPU and GPU, which they don't take kindly to - the CPU submits the draw calls to the driver, then has to wait for the GPU to finish rendering, then you need to transfer data the "wrong way" across the bus (Which is generally substantially slower than pushing data to the GPU), and wait for the transfer to complete before carrying on.

There might be other ways to do what you're trying to do to eliminate the GPU -> CPU transfer - what exactly are you trying to do?

#3 ctMarine2009   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 July 2010 - 08:44 AM

thanks for the quick reply steve.

Basically this is what I'm planning to do. I've got one program that wants the depth buffer in fp32 format that does its own thing. I've got a scene generator that produces the scene, entities, and the depth buffer. I need to pipe the depth buffer from the scene generator to the other program so I'm simulating this (I think) by writing it(the buffer) to a file. this also gives me a chance to view it in my home-made bin viewer to make sure that the outputs are coming out correctly.

Currently I'm doing as you say. Pixelshader writes the value to a render target then I lock it and memcpy it. I'll try the double buffer, I don't really mind a frame of lag I just can't afford to into the the teens fps waiting on the lock to be free.

#4 MJP   Moderators   -  Reputation: 11786

Like
0Likes
Like

Posted 13 July 2010 - 10:04 AM

Don't lock the render target, copy it to a surface in D3DPOOL_SYSTEMMEM using GetRenderTargetData and then lock the system surface.

#5 ctMarine2009   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 July 2010 - 10:48 AM

Sorry I misspoke(misstyped) there. This is the snippet that is bogging down my program.


LPDIRECT3DSURFACE9 lpSurf;
lpD3DDevice->CreateOffscreenPlainSurface(viewport.Width, viewport.Height,
D3DFMT_R32F, D3DPOOL_SYSTEMMEM, &lpSurf, 0);
lpD3DDevice->GetRenderTargetData(lpDepth, lpSurf);

// Now we can access the system memory surface to read our depth values
D3DLOCKED_RECT lockData;
lpSurf->LockRect(&lockData, NULL, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK);
memcpy(toWorkerRange,lockData.pBits,viewport.Width*viewport.Height);
lpSurf->UnlockRect();
lpSurf->Release();




#6 ctMarine2009   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 July 2010 - 11:48 AM

So this is what I understand from what you were saying wrt double buffering the depth buffer.

My card can support up to 4 render targets so theoretically I should be able to have 3 depth buffer targets with the first of the four being devoted to the frontbuffer. I could have a variable to control which render target to use each time and send the copy of the surface off to a worker thread to lock then write to a file or pipe to another program. Would this alleviate any of the serialization?

I'm going to attempt to implement this solution please let me know if anyone knows of a more efficient method to use.

Thanks,
Chris

#7 Evil Steve   Members   -  Reputation: 1987

Like
0Likes
Like

Posted 13 July 2010 - 09:23 PM

Quote:
Original post by ctMarine2009
So this is what I understand from what you were saying wrt double buffering the depth buffer.

My card can support up to 4 render targets so theoretically I should be able to have 3 depth buffer targets with the first of the four being devoted to the frontbuffer. I could have a variable to control which render target to use each time and send the copy of the surface off to a worker thread to lock then write to a file or pipe to another program. Would this alleviate any of the serialization?
Actually, what I meant was simpler: Create two render targets buffers (Or, the default one plus one extra) and two system memory surfaces, and call SetRenderTarget() each frame, alternating which buffer you set as your render target. Psuedocode:

LPDIRE3DSURFACE9 g_pRTs[2]; // Assume this is initialised to the two render targets
LPDIRE3DSURFACE9 g_pBuffers[2]; // Assume this is initialised to the two system memory surfaces
int g_nBuffer = 0;

// Render loop:
lpD3DDevice->SetRenderTarget(g_pRTs[g_nBuffer]);

// Render here

// Read data to system memory surface
lpD3DDevice->GetRenderTargetData(g_pRTs[g_nBuffer], g_pBuffers[g_nBuffer]);

// Lock the surface used on the PREVIOUS frame
int nPrevIdx = 1 - g_nBuffer;
D3DLOCKED_RECT lockData;
g_pBuffers[nPrevIdx]->LockRect(&lockData, NULL, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK);
memcpy(toWorkerRange,lockData.pBits,viewport.Width*viewport.Height);
g_pBuffers[nPrevIdx]->UnlockRect();

// Move to next buffer
g_nBuffer++;
if(g_nBuffer >= 2)
g_nBuffer = 0;


By locking the buffer used on the previous frame, you give the driver more time to get the data transferred to the system memory copy - when you LockRect() the surface, the driver has to make sure that it's finished with the surface so it can yield it to the CPU. It introduces a frame of lag because you're processing the data from the previous frame, but it should improve performance.

Also, in the code you posted before, you're calling CreateOffscreenPlainSurface every frame - that would be pretty bad for performance too - create everything up front, and don't create anything at all in your render loop if you can possibly help it.

Steve Macpherson
Systems Programmer

Rockstar North


#8 ctMarine2009   Members   -  Reputation: 100

Like
0Likes
Like

Posted 14 July 2010 - 07:28 AM

I appreciate the help Steve I didn't realize that I was creating a texture every time. I bumped up from 3 fps to around 10 with that and the double buffering trick.

Now a theoretical question. If I stopped caring about the frame lag I could set up more buffers and cycle through them with the same concept as the double buffering correct?

Thanks again,
Chris

#9 Evil Steve   Members   -  Reputation: 1987

Like
0Likes
Like

Posted 14 July 2010 - 09:53 PM

Quote:
Original post by ctMarine2009
Now a theoretical question. If I stopped caring about the frame lag I could set up more buffers and cycle through them with the same concept as the double buffering correct?
Yes, although you'll reach a point where it won't get any faster, since there's only so much data you can transfer across the PCI-E bus. You'd probably need to experiment to find how many buffers give you the highest framerate.

#10 ctMarine2009   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 July 2010 - 06:46 AM

Steve,

I really appreciate your help. I used multiple buffers and was able to get my framerate all the way up to 25. Thats a winner in my book.

Thanks again,
Chris

#11 sayezz   Members   -  Reputation: 123

Like
0Likes
Like

Posted 17 January 2013 - 08:02 AM

Hey Chris,

do you still have the source code of your application? I tried the way described here, but there was no increas in the frame rate.

Thanks!

 

Regards

Denis






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