Tuesday, March 23, 2010

Instancing and Geometry Shader

ViruZ uses Instancing to draw its graphics.
Instancing is quite simple: You have one piece of geometry (e.g. a rectangle) and you wanna draw it multiple times.
Normally you'd just be like execute the draw-function n times, but Instancing moves this to the graphics-card, meaning that the CPU is free to do more stuff while the graphics card handles drawing the rectangle n times.

This worked quite out-of-the-box for me. Good stuff.
Here's a screenshot:
It has my circle-shader, lightly modified. You can see that the edge of the circles are blurred a bit too much; I've simply been guessing the value then, it's fixed now (at least for 1:1 ratio resolutions..).
I had to turn VSync off in my drivers to get the read FPS (the numbers in the console-window on the left). As you can see, the performance improved greatly (from ~100-120FPS to ~1900-2000FPS). I'm hoping this will make my game run smoother on older hardware.
There are not all 50 viruses visible, because the boundaries are now invalid (the canvas is now -/+1 to -/+1 instead of 0/0 to 800/600 :P).

You might have heard about geometry shaders. They take vertices (points, that define geometry) and may output more vertices in return.

Before that, I've been uploading a quad (rectangle) into the graphics-memory, had a static array of 50 positions (representing the origins of the viruses) and then used Instancing to draw that quad 50 times. The shader took care of translating that quad onto the right position.

Now, with a geometry shader, I'm not uploading any geometry into the graphics-memory, but instead have a dynamically sized array of 2D points (again representing the origins of the viruses) and using that information for Instancing to draw.. well points ^^

I could be satisfied with points for now, but I'm planning on implementing more viruses with different shapes at some point, thus only squares will not always be the best solution.

My geometry shader takes those points and creates 4 vertices for each point by simply translating it to the lower left, then upper left, then upper right and finally lower right, resulting in a square shape.

I had some trouble with this geometry shader, mainly because I've accentually been trying to compile my pixel shader as a geometry shader and it took quite a while to find out that the shader code was actually correct XD

And again a picture:
You can see that I've also implemented the outline of the virus.
It also seemed to slightly have improved my performance to ~2000-2050FPS.

I didn't have too much time, so I spend it on the stuff you can experiment with, instead of stopping in the process of cleaning up a few times.
So yeah, the same excuse goes for the blogpost-less week. Now I've left the written parts of my Abitur behind. Coulda done the math-test better; was spoilt by the really easy CS-test. That thing felt like a joke.


LordHelmchen said...

Hi, kannste das zum Download bereitstellen? W├╝rde es gern ma ausprobieren ;)

z33ky said...

I plan on releasing a pseudo-demo soon, I just wanna switch to the OpenGL 1.1 core properly (for older hardware) and maybe get anti-serum to work again.

This shouldn't take too long if not my computer had decided to break down and only displays in console-mode. I need to find out the cause (atm suspecting the graphics card) and replace the faulty hardware.
I wanted to wait some more for a completely new computer... GPGPUs sound awesome ;)