2
\$\begingroup\$

I'm using a few different render targets in my game in addition to the back buffer. These other render targets are only used in a few places, for specific tasks.

I'm wondering when I should be clearing them. Right now I clear all of my render targets at the beginning of the frame, and it seems like I'm waiting for all the textures to clear before the rest of the drawing gets underway.

Would it be more efficient to clear these textures later in the frame, when they aren't being used? Is there any hope of the GPU sort of clearing them "on the side" while unrelated rendering is happening? Or are these tasks always sequential and will I always need to wait for clearing?

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you profiled to see how much time the clears take on the GPU? Clears are normally very fast. I'd be surprised if they're taking enough time to be worth worrying about. \$\endgroup\$ – Nathan Reed Nov 13 '13 at 23:06
  • \$\begingroup\$ @Nathan Yeah, reason I ask is cause I was looking at an experiment in PIX and the clears were taking up a decent chunk of the beginning of the frame. That said- I was running at about 150fps in that scene so I'm not exactly scraping the bottom of the optimization barrel with this one. I guess I'm asking more for general education than I am out of need. \$\endgroup\$ – Raptormeat Nov 13 '13 at 23:20
  • 1
    \$\begingroup\$ That's most likely not actually the clears; you'll find that the first D3D call in a frame almost always has a large time logged in PIX, and you can test this by putting any other call first. \$\endgroup\$ – Maximus Minimus Nov 14 '13 at 0:52
1
\$\begingroup\$

Clear them when you need them cleared.

As discussed in the comments, the performance degradation you are seeing is likely related to the placement of the calls in the relative call stream for the frame, and not specifically the fact that clearing the buffer is "slow" (it should be one of the faster operations available, since implementers know how often it will be called).

But it will take non-zero time, and there's really no point to clearing the buffers until you need to. So, if you are rendering to them every frame, clear them every frame. But if you only render to them once every few frames, you should consider moving to clear to just before the render into that buffer. It's possible this will, itself, have performance implications based on how you are using all the other related graphics objects, including the buffer itself, so you'll need to experiment and verify; perhaps you should clear immediately before, perhaps immediately after you're done using the buffer, and so on.

If there's no measurable performance difference, favor the option that is most logical.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.