Present() without actually presenting..

Started by
10 comments, last by Giallanon 12 years, 2 months ago
In my understanding, the only way to be sure that the GPU has finished all his jobs, is to call Present().
Present will stall until everything gets done.

Anyway, when you call Present(), the current backbuffer is flipped to frontbuffer showing whatever you've draw onto it.
What if I don't want to show anything?
Or better, what if I want to let the current fontbuffer unchanged (ie: don't want the flip to occur) ?

For example, let's say I need to fill a texture. I can do it by using a rendertarget and doing some gpu works on it.
Now I want to wait (or to be notified when..) until the job on the rendertarget has finished so that I can use it for something else.

Is there any way to do this?
Advertisement
You shouldn't have to worry about it - the driver will internally block when you try to use the rendertarget (E.g. if you LockRect() it or something), and wait for previous rendering to complete.
If you need to explicitly flush the command buffer to the GPU and then wait for it to idle, you can submit a query and retrieve it's result using [font=courier new,courier,monospace]IDirect3DQuery9::GetData(D3DGETDATA_FLUSH)[/font] (in the docs, see "Accurately Profiling Direct3D API Calls (Direct3D 9)" and "Queries (Direct3D 9)").

However, if "use it for something else" is more GPU work, you don't have to do anything at all - it will work as expected.
Also, if you just want to read the texture back to the CPU, then simply lock the texture - the driver won't let the lock complete until the previously issued commands affecting the texture have been completed.
Thank you for answers.
LockRect() could be an option to be sure that GPU has finished his works, but what I'm trying to do is a bit more complex.

I've a thread that will BeginScene() -> Do GPU Jobs -> EndScene() -> Present as soon as there are some GPU jobs scheduled.
(BTW: this is hypotetical, I'm just trying to figure it out).

Suppose I want to render the main game scene only every 16ms (60fps).
The idea is that at time=0 I submit a GPU job that will render the scene and present it to frontbuffer.
Then, the GPU will do nothing for the next 16ms until, at time=16ms, I will submit another GPU job.
This will go on forever.

Now, if during the "idle" time I need to submit another GPU job (ie: render to a texture), the problem I'm facing is that the main thread will BeginScene() -> do job -> EndScene() and, finaly, Present().
The last Present() will actually present a "not rendered" backbuffer, while I'd like to let the current frontbuffer untouched.
The fact that you've mentioned using a D3D9 device from two different threads is a huge red flag (D3D9 is not designed to be used by any thread other than the one that created your window) -- why do you need two different threads to be able to control the device?

Why can't you do your RTT in the same begin/end/present bundle?

i.e [font=courier new,courier,monospace]BeginScene -> RTT -> Main GPU Jobs -> EndScene -> Present[/font]




As a side note, the GPU is allowed to lag behind the CPU by an arbitrary amount of time. When you issue some D3D commands, the GPU might not start working on them until, say 30ms later. If you're trying to keep the CPU and GPU tightly in sync, then you'll end up causing a lot of stalls as you continually wait for these latencies.
Usually the GPU is one frame behind, so when you're submitting the commands for frame #2, the GPU is just starting to execute the commands from frame #1. So, Present doesn't really guarantee that the GPU has finished all of it's commands at all, all it does is submit a command to the GPU (which will be executed in the future, e.g. with a 1-frame delay) telling it to display the results of it's most recently-completed frame. If the CPU is running faster than the GPU and is getting too far ahead, then Present will stall, to avoid getting into a situation where you've buffered up 100 frames worth of commands, etc...

Why can't you do your RTT in the same begin/end/present bundle?
i.e BeginScene -> RTT -> Main GPU Jobs -> EndScene -> Present

Main GPU Jobs is unkwnown at the moment. It has been drawn 16 ms ago, so when Present() will end, what will it present? It will present the current backbuffer which should be "dirty" because during this BeginScene() / EndScene() I didn't write anything on it, while I'd like to keep the current frontbuffer untouched (ie: still show the result from the previous BeginScene() / EndScene() / Present())



Also, the fact that you've mentioned using a D3D9 device from two different threads is a dangerous sign -- why do you need two different threads to be able to control the device?



I've simplified a bit the whole thing, but I'm not accessing D3D from multiple threads. Only main thread directly access D3D.
Other threads sends commands to a queue and then the main thread will execute them.
You should figure it this way: there is a PC somewhere in the world that can render things using DX9.
There's another PC in the world that communicate with the first one and ask for jobs to be done and then read back the results.
Don't think about threads, this is not the point.
Ok, the logic I was using was that you've got this:
[font=courier new,courier,monospace]BeginScene -> Main GPU Jobs -> EndScene -> Present -> BeginScene -> RTT -> EndScene -> Present[/font]
But you don't want the last present to occur:
[font=courier new,courier,monospace]BeginScene -> Main GPU Jobs -> EndScene -> Present -> BeginScene -> RTT -> EndScene[/font]
Which lets you simplify it to this:
[font=courier new,courier,monospace]Main GPU Jobs -> EndScene -> Present -> BeginScene -> RTT[/font]
Which is the same as this:
[font=courier new,courier,monospace]BeginScene -> RTT -> Main GPU Jobs -> EndScene -> Present[/font]
You should figure it this way: there is a PC somewhere in the world that can render things using DX9.
There's another PC in the world that communicate with the first one and ask for jobs to be done and then read back the results.
Do you need to download the results of the jobs before you can begin the next "Main render job"? Or are the "main render job" and the "during idle" jobs independent of each other?

Do you need to download the results of the jobs before you can begin the next "Main render job"? Or are the "main render job" and the "during idle" jobs independent of each other?

The idea is to have "Main job" and "Idle jobs" totally indipendent from each other.
Also there should not be a real difference between "main job" and "idle job" (from the GPU point of view) apart from the fact that "main job" wants to Present() the result to the frontbuffer while "idle job" doesn't


Ok, the logic I was using was that you've got this:
BeginScene -> Main GPU Jobs -> EndScene -> Present -> BeginScene -> RTT -> EndScene -> Present
But you don't want the last present to occur:
BeginScene -> Main GPU Jobs -> EndScene -> Present -> BeginScene -> RTT -> EndScene
Which lets you simplify it to this:
Main GPU Jobs -> EndScene -> Present -> BeginScene -> RTT
Which is the same as this:
BeginScene -> RTT -> Main GPU Jobs -> EndScene -> Present

Yes, this could be true if "main job" and "idle job" were always both queued for scheduling, but this is not always the case.
I'm not sending the "main gpu job" every frame, I'm sending it every now and then.
Using big numbers just to be clear, let's say I send a "main gpu job" every 10 seconds; GPU receive it and display it using Present().
For the next 10 seconds, the front buffer should never change, it must display the result from the last "main job" BUT, I could send "idle jobs" to be executed and I don't want to wait the next "main job" before they get exectued.
So just don't call Present unless a main job has been submitted? Or am I missing something here?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


So just don't call Present unless a main job has been submitted?

I remember I read something about DX9 doesn't like multiple BeginScene() / EndScene() without a Present in the middle.
Also (but this is a minor issue that can be solved with a LockRect as stated in the 2nd post) without calling Present() you can't be sure that after EndScene() the GPU will flush the pipeline and actually do its job

This topic is closed to new replies.

Advertisement