Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

Direct3d would there be a problem in putting the begin/end scene around my main loop?

This topic is 4814 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

Hi, I was wondering with direct3d if its fine to surround my main loop with the begin/end scene functions, so I can call draw from anywhere in my program? eg.
d3dDevice->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
d3dDevice->BeginScene();
//main loop
input.run();
ships.run();
space.run();
//...etc
d3dDevice->EndScene();
d3dDevice->Present(0,0,0,0);

Thanks

Share this post


Link to post
Share on other sites
Advertisement
I don't think there'd be a problem, but it's usually best to handle your game updates and then do the rendering wherever possible, since that allows you to update any resources (e.g. dynamic vertex buffers), and give the machine a bit of time to send that data to the card before it's needed. If you lock-fill-unlock a vertex buffer, and then try to render from it, you'll cause a stall while the data is transferred onto the GPU.

Also, I believe there's a few functions that can't be called from within a scene, but I don't think there's any that really matter (E.g. Present(), Reset() and so on).

Share this post


Link to post
Share on other sites
I think Evil Steve is right on this one. If I understand correctly, when you call BeginScene and EndScene, it would spend all that time communicating between the GPU and CPU. For example, if you were to do all your physics calculations (or other heavy CPU stuff) it would drag down the performance. If I remember correctly, it is actually a good idea to call BeginScene and EndScene at the beginning of the loop (thus throwing everything at the GPU) then doing the work on the CPU. That way the GPU munches away on everything that has been thrown at it while the CPU can go ahead and do other things. If the BeginScene and EndScene calls are made at the end of the program loop it has to wait for the driver to finish its job before it can loop again. At least that is what I have heard. I am not sure how reliable it is though. Anyone?

Share this post


Link to post
Share on other sites
There seems to be some debate over how to properly do a BeginScene/EndScene. The DirectX docs state that the ideal case is to have exactly one BeginScene/EndScene pair for each Present call, implying that there should only be one pair per frame. But what about updating multiple render targets per frame? Is it better to do:

BeginScene
for each render target
{
SetRenderTarget
UpdateTarget
}
UpdateMainScene
EndScene
Present

or

for each render target
{
SetRenderTarget
BeginScene
UpdateTarget
EndScene
}
BeginScene
UpdateMainScene
EndScene
Present

The first case complies with the MS recommendation. The second case works better for tile-based architectures like the GMA900/950. Which is the best general case?

Share this post


Link to post
Share on other sites
There isn't a tremendous amount of activity going on inside of BeginScene()/EndScene(). Realistically, the bulk of the processing is in Present(), which is usually only called once per frame, anyways.

Quote:
If I understand correctly, when you call BeginScene and EndScene, it would spend all that time communicating between the GPU and CPU. For example, if you were to do all your physics calculations (or other heavy CPU stuff) it would drag down the performance.

I don't think this is quite how it works. When an application wants something done (like a state change for example), it submits a token into the driver command stream. This stream is buffered, so it can take a little while for the driver to get to it.

Your BeginScene()/EndScene() status has no bearing on GPU commands being executed - just commands being placed. Remember, the whole architecture here is asynchronous, so stuff doesn't just happen when you call the function.

In reality, the whole CPU-GPU architecture is pretty much a one-way thing. This architecture was not engineered for the GPU to be communicating with the CPU (hence, things like locking textures takes frickin forever). Instead, the CPU communicates with the software driver, which in turn communicates with the GPU. The exception is with the newly introduced (as of DX9) query system, which are still asynchronous.

I have one big Begin/End pair running throughout my loop. This way, if other components want to render stuff (such as text or UI elements), they can.

Share this post


Link to post
Share on other sites
Hi there johnnyBravo,
How are you doing?

The Problem
Loop with Begin Scene and End Scene on the edges.

The Solution
I would say that when structuring your engine it's best to leave rendering to the renderer. Although your approach wouldn't cause any problems..

Something like.
Engine::Run()
{
Renderer::RenderEntities();
Physics::Run();
Input::Run();
}

And you could just place your engine's run command in your loop like
while(1)
{
if(Window.checkMessages() == -1)
break;
Engine->Run();
}
Or whatever :)

The main thing is that your loop wouldn't cause any problems but it might become tedious in the end when you want to seperate your renderer which will internally call beginscene and endscene.


I hope this wasn't too confusing.
Take care buddy.
I hope all goes well

Share this post


Link to post
Share on other sites
I see, before I was letting each of my classes send themselves to the renderer,

But now I'll make the renderer go through the list of objects and draw them.

Thanks for clearing this up guys!

Share this post


Link to post
Share on other sites
There is no problem doing what the AP said. But it doesn't lead to a good structuring of code inot distinct domains - updating the world, and rendering it.
If you want to be able to decide to render at any point, then you probably should be buffering the things to render using a scene graph.

Share this post


Link to post
Share on other sites
Thanks, I just read up an article from game dev on scene graphs, they seem very useful, i might attempt to implement later on. As of right now, I'm just going to loop through all the objects/effects and draw them.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!