So I’ve been worrying about how well I draw my sprites in my little home game engine. I began researching it and began to learn about something called Instancing, which is a way to draw a whole lot of things without killing your computer.
For the non-technically literate the basic way that you draw something using a computer is by setting up all the data for the thing you want to draw (position, what image it is, size, etc). You then send that data to the video card on your machine. Finally you tell it to draw the stuff you sent it. Simple, right? Not quite.
Sending things to the video card is hard for the computer, and drawing things is even harder. So a key part of rendering is figuring out ways to minimise both the sending of data to the GPU, and also the amount of times you ask it to draw something. A great way to do this would be fewer draw calls. I mean, if you were drawing the same thing over and over again, just in different positions, it would only make sense to do it in one draw, right? Right. Instancing is a way to do this.
The basic idea behind it is to hand the GPU a bunch of data that’s the same for every instance you’re going to draw, lets call it SAME_DATA. This would be the shape if it were a 3D object, so all the things that describe that object, like the position of vertices, texture coordinates, etc. You then also hand it a bunch of data that holds the things that are different for each instance, lets call it DIFFERENT_DATA. When it comes time to draw something, the GPU will draw the SAME_DATA stuff the same way for every instance, but will keep peeking into the DIFFERENT_DATA to grab stuff that’s different for each one. So that way you can get the same exact 3D model drawn in a bunch of different places using only one draw call.
So that’s how it works. I have decided not to use it just yet.
There are a couple of reasons for this.
The minor reason is that there might be some evidence that it’s not that helpful unless you’ve got an absolute ton of things being drawn.
The major reason is that I don’t want to. I’ve been messing around with the rendering side of my engine for a while now. I most recently added support for texture atlasing and reworked how sprites operate and how they animate. This has had me make several significant changes to how the rendering works. I want a chance to solidify what I’ve done before I start doing another major overhaul. There’ll be plenty of time to improve it later, but it’s not even currently a problem, so it’s on the backburner for now.