Jump to content

  • Log In with Google      Sign In   
  • Create Account


Present() without actually presenting..


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

#1 Giallanon   Members   -  Reputation: 1065

Like
0Likes
Like

Posted 10 February 2012 - 06:09 AM

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?

Sponsor:

#2 Evil Steve   Members   -  Reputation: 1955

Like
0Likes
Like

Posted 10 February 2012 - 06:19 AM

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.

Steve Macpherson
Senior programmer, Firebrand Games


#3 Hodgman   Moderators   -  Reputation: 27690

Like
0Likes
Like

Posted 10 February 2012 - 06:36 AM

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 IDirect3DQuery9::GetData(D3DGETDATA_FLUSH) (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.

#4 Giallanon   Members   -  Reputation: 1065

Like
0Likes
Like

Posted 10 February 2012 - 07:31 AM

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.

#5 Hodgman   Moderators   -  Reputation: 27690

Like
0Likes
Like

Posted 10 February 2012 - 08:20 AM

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 BeginScene -> RTT -> Main GPU Jobs -> EndScene -> Present



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...

#6 Giallanon   Members   -  Reputation: 1065

Like
0Likes
Like

Posted 10 February 2012 - 08:27 AM

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.

#7 Hodgman   Moderators   -  Reputation: 27690

Like
0Likes
Like

Posted 10 February 2012 - 08:36 AM

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

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?

#8 Giallanon   Members   -  Reputation: 1065

Like
0Likes
Like

Posted 10 February 2012 - 09:11 AM

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.

#9 mhagain   Crossbones+   -  Reputation: 7436

Like
0Likes
Like

Posted 10 February 2012 - 09:40 AM

So just don't call Present unless a main job has been submitted? Or am I missing something here?

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#10 Giallanon   Members   -  Reputation: 1065

Like
0Likes
Like

Posted 10 February 2012 - 09:44 AM

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

#11 Hodgman   Moderators   -  Reputation: 27690

Like
0Likes
Like

Posted 10 February 2012 - 09:25 PM

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().

I still don't see why you can't put both jobs into the same begin/end scene pair, like:
void main()
{
  beginScene();
  while(!quit)
  {
    if( maybe1 )
      doIdleJob();
    if( maybe2 )
    {
      doMainJob();
      endScene();
      present();
      beginScene();
    }
  }
  endScene();
}

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

Why do you need to manually flush the pipeline and ensure that the GPU has completed it's job? That detail is automatically managed -- the job will be completed before you use it's results. If you don't use it's results, you can't be sure it's completed yet, but you don't need to be sure because you're not using the results...

#12 Giallanon   Members   -  Reputation: 1065

Like
0Likes
Like

Posted 11 February 2012 - 05:34 AM

Yes, I think you're right. I just have to beginscene() and let it open until someone want to present something to the screen. When this happen just endscene() and present(). Thank you for helping




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