Jump to content
  • Advertisement
Sign in to follow this  
RobMaddison

Rendering to and reading from a texture (or two)

This topic is 2036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I rendered to a texture during one frame and then wanted to read from that texture would I have a big performance hit? If so, what about if I had two render targets and I rendered to one of them during a frame and read from the other in a flip flop fashion, would I still incurr an "across the bus" penalty?

Share this post


Link to post
Share on other sites
Advertisement

I'm not sure what you mean by "big performance hit", but writing to a texture and then reading from it later is a very common operation that a GPU can certainly handle. In general the performance will be dictated by the bandwidth available to the GPU, so higher-end GPU's will take less of a performance hit. Switching render targets every frame won't save you any bandwidth, and you'll just end up using more memory.

Share this post


Link to post
Share on other sites
Thanks MJP

I have been writing a software rasterizer to do some occlusion culling and it seems to work ok(ish) but then I thought it might be quicker to render my occlusion polygons (using different block colours) to the same-sized render target (256 pixels wide), then lock the texture to get access to the data, loop through each pixel adding its colour to a palette and then indexing that palette for visibility.

Sound reasonable?

Share this post


Link to post
Share on other sites

I've just set this up and I'm getting an error locking (to access pixels) my rendertarget, DX in debug says

 

"Lock is not supported for textures allocated with POOL_DEFAULT unless they are marked D3DUSAGE_DYNAMIC"

 

I tried creating the texture with RENDERTARGET and DYNAMIC but DirectX won't allow it.  So is this possible somehow?

Share this post


Link to post
Share on other sites
The penalty incurred by GetRenderTargetData will depend on the amount of latency between the CPU and the GPU. Usually, commands are buffered in such a way that the GPU will be running one or more frames behind the CPU. i.e. you submit a draw call on the CPU and the GPU might not actually act on that command for 30ms+...
If you ask for some results immediately after asking the GPU to generate those results, then the CPU is forced to stall, sitting around waiting for the GPU to catch up and do the work. If you do this every now and then, you'll likely get a huge stall, as there could be 30ms+ of latency in the queue. If you do this every single frame, then the stalls will be smaller, as the CPU won't have had a chance to 'get ahead' of the GPU by too much. However, the downside here is that when there is not much latency between the CPU and GPU, then the GPU can run out of work to do, and it ends up stalling.
e.g. say that every frame the CPU submits 4 different bits of work for the GPU to do, and then does all the other stuff required for a frame.
A normal situation looks like the left side.
A situation where the CPU/GPU are forced to be in sync looks like the middle columns -- without that extra latency, there's much more chance that the GPU is going to end up waiting on the CPU to feed it more work.
A situation where the CPU wants to read back the results of the first draw work before continuing looks like the right side. The CPU has to wait on the GPU to finish this work, and then the GPU also suffers a stall while it waits for the next task from the CPU to arrive.
  With latency         |   Without latency      |   With read-backs      |
  CPU       GPU        |   CPU       GPU        |   CPU       GPU        |
draw 1   |             | draw 1   |             | draw 1   |             |
draw 2   |             | draw 2   | draw 1...   | [stalled]| draw 1...   |
draw 3   |             | draw 3   | draw 1.     | [stalled]| draw 1.     |
draw 4   |             | draw 4   | draw 2...   | draw 2   | [stalled]   |
physics  |             | physics  | draw 2.     | draw 3   | draw 2...   |
gameplay |             | gameplay | draw 3.     | draw 4   | draw 2.     |
audio    | draw 1...   | audio    | draw 4...   | physics  | draw 3.     |
etc      | draw 1.     | etc      | draw 4.     | gameplay | draw 4...   |
---------+ draw 2...   | ---------+-----------  | audio    | draw 4.     |
draw 5   | draw 2.     | draw 5   | [stalled]   | etc      +-----------  |
draw 6   | draw 3.     | draw 6   | draw 5...   | ---------+ [stalled]   |
draw 7   | draw 4...   | draw 7   | draw 5.     | draw 5   | [stalled]   |
draw 8   | draw 4.     | draw 8   | draw 6.     | [stalled]| draw 5...   |
physics  +-----------  | physics  | draw 7.     | [stalled]| draw 5.     |
gameplay | draw 5...   | gameplay | draw 8...   | draw 6   | [stalled]   |
audio    | draw 5.     | audio    | draw 8.     | draw 7   | draw 6.     |
etc      | draw 6.     | etc      +-----------  | draw 8   | draw 7.     |
---------+ draw 7.     | ---------+ [stalled]   | physics  | draw 8...   |
draw 9   | draw 8...   | draw 9   | [stalled]   | gameplay | draw 8.     |
draw 10  | draw 8.     | draw 10  | draw 9...   | audio    +-----------  |
draw 11  +-----------  | draw 11  | draw 9...   | etc      | [stalled]   |
draw 12  | draw 9...   | draw 12  | draw 9.     | ---------+ [stalled]   |
physics  | draw 9...   | physics  | draw 10...  | draw 9   | [stalled]   |
gameplay | draw 9.     | gameplay | draw 10.    | [stalled]| draw 9...   |
audio    | draw 10...  | audio    | draw 11.    | [stalled]| draw 9...   |
etc      | draw 10.    | etc      | draw 12.    | [stalled]| draw 9...   |
                                                | [stalled]| draw 9.     |
                                                | draw 10  | [stalled]   |
                                                | draw 11  | draw 10...  |
                                                | draw 12  | draw 10.    |
                                                | physics  | draw 11.    |
                                                | gameplay | draw 12.    |
                                                | audio    +-----------  |
                                                | etc      |

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!