Do all 2D games do this?

Started by
16 comments, last by frob 9 years, 10 months ago

Furthermore, if you are concerned about drawcalls I would like to see how this function is implemented.


blockAnim.draw(g2D,camera);

Just from looking at that line I suspect there is much room for improvement.

Sure. You can see how I implemented it. I had to override the draw method.

 
for(int i = 0; i < getListSize() ;i++)
{
 
if(getCurrentFrame() <= getFrameEntries(i))
{
               g.drawImage(getImage(i), getX() - camera.getX()  , getY() - camera.getY() , null);
               break;
               }
}
Advertisement

I hear draw calls are expensive...

What graphics API is being used to draw your tiles?

The "draw calls are expensive" primarily relates to DirectX/OpenGL. If you are using an intermediary API, it's quite possible it is batching up draws already, and thus may have completely different performance characteristics.

I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API?


I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API?

Exactly that: the AWT canvas/bufferstrategy API (as opposed to using OpenGL directly, for example).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API?

Exactly that: the AWT canvas/bufferstrategy API (as opposed to using OpenGL directly, for example).

Oh so I don't need to worry about efficiency then since the API is batching it up like that?

I have no idea - AWT isn't exactly my area of specialty. I'm sure there are some AWT-specific performance guidelines floating around out there...

If you were using OpenGL, I could offer performance advice :)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

In the current APIs individual draw calls are expensive because for each draw call the API/Driver are potentially changing and verifying a lot of state. Batching similar draws (say, all the grass tiles, or better yet, all the tiles from the same, large texture atlas, using the same shader, etc) is one way of reducing the number of draw calls you have to make -- thus, the API/Driver overhead is amortized across all those tiles that would otherwise be drawn individually in a naive renderer. Realistically, on a modern desktop or laptop with current APIs you get a couple thousand draw calls before your CPU is completely swamped by the overhead. If you have a game running at just 640x480, using small tiles of 16x16 pixels, drawing just one densely-populated layer of tiles consumes 1200 draw calls if you do them individually. Figure two more sparsely-populated layers for objects and overhead graphics add 50% on top of that. You're 640x480 game has already consumed half of available draw calls per frame -- Now draw lots of characters, throw in some particles and UI -- you're already probably at or around the comfortable limits if your game does any interesting processing, and you haven't drawn a single off-screen tile or entity. Drawing half a screen-width extra in all directions multiplies the cost by 4x and you're way over your draw call budget.

On mobile platforms using mostly OpenGL ES, you can expect to make half or fewer draw calls to stay in budget.

Its mostly batching that's important if you're using a 3D API -- Once the GPU gets a hold of the draw call it'll chew through clipped pixels like nobody's business, and it'll reject them before running expensive pixel shader code. There's no point sending stuff to the GPU that you easily know is not in view, but you don't have to worry about being tile or pixel-perfect about it. Batching will save you far more.

As an aside, new style APIs like Mantle, D3D12, and the console APIs aren't so affected by draw call counts, and have other features to keep re-usable draw commands on the GPU to reduce overhead even further. In statistical analysis, the D3D12 team showed that nearly all games re-use 90% of their draw commands frame-over-frame, so my understanding is that these APIs make it possible to just re-use the command with slightly different properties (say, its transform matrix or lighting properties.), rather than rebuilding the command, sending it to the GPU and verifying it each frame.

throw table_exception("(? ???)? ? ???");

Using OpenGL I make 2D tile maps with a single triangle-strip and degenerate vertices. Don't know if it's the best practice but it gets the job done.


I'm using Java Standard: more specifically the awt library that has Canvas and BufferStrategy and Image and BufferedImage. What do you mean by intermediary API?

Ouch. Be careful.

That relies heavily on the Java environment it is being run on. As you probably aren't doing this on Android (I'm sure you would have tagged it and placed it in the Mobile forum) you should know some quirks.

The Windows JRE has some nasty performance issues in games rendering. Canvas is sometimes hardware accelerated, and sometimes not. BufferedImage particularly is notorious for switching between accelerated and non-acclerated. It can do it for several well documented reasons, such as reading from the image (hardware acceleration is write-only), or resizing the window, or moving between screens, or having Windows switch video modes. It can do it for many reasons which are seemingly random, such as another program running in the background.

The runtime environment can --- for no reason your program can detect --- switch out of hardware acceleration mode. This will drop your framerate from something high (e.g. 3ms per frame) to something extremely low (e.g. 729ms per frame). This can happen at any time without any automatic notification.

You can monitor it yourself and suddenly notice you switched from frames-per-second to seconds-per-frame, but even once you detect it there is no way to correct it.

This is one of the biggest compelling reasons that people use external libraries for game graphics. Java's AWT works mostly okay for slow business graphics where a visual update can take a few moments. If you are making chess or something with slow event-driven updates you might be fine. But if you need a solid frame rate measured with consistent double-digit milliseconds, you will not get that with AWT.

This topic is closed to new replies.

Advertisement