2
\$\begingroup\$

How can any interactions between these two shaders work? I mean the vertex shader sends a vertex's color to the fragment shader, but that's only one fragment, what about all the other ones between the vertices?

Does the fragment shader get called a lot more than the vertex shader?

I mean the fragment-to-vertex ratio is pretty high in most games, given that a fragment is a pixel, right?

\$\endgroup\$
1

1 Answer 1

5
\$\begingroup\$

Vertex shaders gets called a lot less than fragment shaders. It's because the fragments get generated from the vertices with a process that is called rasterization. Rasterization basically generates fragments using the triangle's corners and give each fragment an interpolated value using the values in the vertices, produced by a vertex shader.

Let's say that you have a triangle ABC like this:

a = 0,0,0 
b = 1,0,0 
c = 1,1,0

Depending on the resolution, this generates some amount of fragments covering the whole triangle.

This will produce a triangle which covers half of the screen, which at 1920x1200 resolution will be 1920*1200/2 fragments. The amount of fragments increase with every triangle you draw.

A fragment is not a pixel. It is much more.

\$\endgroup\$
6
  • \$\begingroup\$ I mean, if I say gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); it makes all fragments between vertices red. Then why gl_FragColor = vColor; where vColor is the vertex shader's passed vertex color, make a nice gradient between vertices? \$\endgroup\$
    – Kipras
    Commented Nov 14, 2013 at 19:50
  • \$\begingroup\$ I edited. Does it now answer your question? \$\endgroup\$
    – Lasse
    Commented Nov 14, 2013 at 19:52
  • \$\begingroup\$ Oh so, the vertex color stays the same and is applied to all fragments, just it also gets interpolated with other colors to make the gradient? \$\endgroup\$
    – Kipras
    Commented Nov 14, 2013 at 19:54
  • \$\begingroup\$ The colour of the vertex stays the same, and the colour of the fragments is got from interpolating the colour of the vertices. doc.minko.io/wiki/… \$\endgroup\$
    – Lasse
    Commented Nov 14, 2013 at 19:56
  • 1
    \$\begingroup\$ @Lasse why would it be twice the amount of fragments without backface culling? You only render one triangle. If it faces the wrong direction there will be 0 fragments generated. The rasterization pipeline doesn't generate two fragments for each rasterized point of a triangle without backface culling. \$\endgroup\$
    – TravisG
    Commented Nov 17, 2013 at 19:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .