
Here we defined a uniform array called offsets that contain a total of 100 offset vectors. Gl_Position = vec4(aPos + offset, 0.0, 1.0) Nothing new so far, but at the vertex shader it's starting to get interesting: The quads are colored in the fragment shader that receives a color vector from the vertex shader and sets it as its output: Below is the vertex data used for this example - the triangles are small enough to properly fit the screen when there's a 100 of them: Each vertex contains a 2D NDC position vector and a color vector. The result is a neatly organized grid of quads that fill the entire window:Įach quad consists of 2 triangles with a total of 6 vertices. We accomplish this by uniquely positioning each instanced quad by indexing a uniform array of 100 offset vectors. To get a feel for instanced drawing we're going to demonstrate a simple example that renders a hundred 2D quads in normalized device coordinates with just one render call. Having a unique value per instance means we could now for example index into a large array of position values to position each instance at a different location in the world. If we were to render the 43th instance for example, gl_InstanceID would have the value 42 in the vertex shader. When drawing with one of the instanced rendering calls, gl_InstanceID is incremented for each instance being rendered starting from 0. Rendering the same object a thousand times is of no use to us since each of the rendered objects is rendered exactly the same and thus also at the same location we would only see one object! For this reason GLSL added another built-in variable in the vertex shader called gl_InstanceID. The GPU then renders all these instances without having to continually communicate with the CPU.īy itself this function is a bit useless.

We sent all the required data to the GPU once, and then tell the GPU how it should draw all these instances with a single call. These instanced versions of the classic rendering functions take an extra parameter called the instance count that sets the number of instances we want to render. To render using instancing all we need to do is change the render calls glDrawArrays and glDrawElements to glDrawArraysInstanced and glDrawElementsInstanced respectively. If we were to actually render such a large amount of objects it will look a bit like this in code:įor(unsigned int i = 0 i GPU communications each time we need to render an object. However, the thousands of render calls you'll have to make will drastically reduce performance. Because each leaf is only a few triangles, the leaf is rendered almost instantly.

You'll probably want to draw quite a few of them and your scene may end up with thousands or maybe tens of thousands of grass leaves that you need to render each frame.

Think of a scene filled with grass leaves: each grass leave is a small model that consists of only a few triangles. Say you have a scene where you're drawing a lot of models where most of these models contain the same set of vertex data, but with different world transformations.
