Is creating circles from triangles or drawing them with a fragment shader faster?

1.8k Views Asked by At

I use the language Rust and the Glium library. I want to display a large number of circles on the screen, but I can not decide how I'd better do it.

There is an option to create circles from triangles, or I can draw them with a fragment shader, that is, take the distance from the center of each circle to each point on the screen and if it is less than the radius, then paint it in the desired color. For clarity, here is an example of how I draw one circle:

vec2 point = vec2(200.0f, 200.0f);
float dist = distance(point, gl_FragCoord.xy);

if (dist < 200)
    gl_FragColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);

Which method will work faster? Are there options to do it better? The size and color of each circle will change in the run-time.

2

There are 2 best solutions below

6
On

Well drawing with GPU(shader) should be way faster than making a whole bunch of triangles (if I'm indeed correct myself..)

0
On

Faster?

Faster for CPU?

Totally faster?

Nobody knows your environment. Graphics chip can be VERY powerful. And when you can rationally utilize its power it's possible to you program will be "faster"

When you render bucket of triangles your CPU is doing work for prepare geometry parameters, etc, when you render only two triangles per draw call then work was done on GPU side. But this approach can be harder to implement, because you need to transfer raw circle data (I mean radius and center coordinates) in fragment shader. For small count of circle it's trivial, but not for many. Consider about it.

If you do it with distance field texture, then you must create it on CPU or with different draw call.