'Is the position of BeginDraw() (Direct2D) important when coding?
I am currently learning how to use Direct2D to create an app, and I was wondering if placing a lot of 'non-graphical' code after calling "BeginDraw()" would matter. I do not fully understand what "BeginDraw()" actually does, so my question is mostly regarding time of execution. Does it slow other processes? Is it 'eating' the CPU after being called until "EndDraw()" is called ?
Solution 1:[1]
You don't get an extra cost over the period spent within BeginDraw.
You get a cost for calling it, just like any other function, as it needs to prepare stuff and deal with states. In this respect, less BeginDraw blocks is better than more.
But you don't get a cost by virtue of the block being longer scope. In fact, it might even be beneficial to call it earlier as it gives the Direct2D lib time to do background processing, if needed. Might just involve fetching memory. I'd expect this to be marginal or no impact, but doing it earlier, if that's the only change, can help not hurt.
In practice, try to use as few blocks as possible. And use any command buffering API available as oppposed to immediate draw commands. (sorry for lack of details here, not so familiar with DirectDraw myself, more of an OpenGL/Vulkan dev)
Solution 2:[2]
You'll be pleased to know that BeginDraw isn't consuming CPU time after it is called. Some people profile their game or application and get the impression that calls like ID2DDeviceContext.BeginDraw (it's usually EndDraw actually) and IDXGISwapChain.Present are consuming a lot of CPU. The reality is that it only appears that way because they're two of the most commonly made calls in such an application. There's also a good chance some could be misinterpreting profiling reports.
Put simply, BeginDraw and EndDraw just defines when you're able to make drawing calls to your DeviceContext or RenderTarget. When to call it exactly depends upon what sort of application you have. If it has a game loop, call it at the start of your main Draw method. You can write a whole game that makes only a single call to BeginDraw and EndDraw each.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeffrey |
| Solution 2 | ROGRat |
