# How does the fragment shader get data from the vertex shader?

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?

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.

• 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? – Kipras Nov 14 '13 at 19:50
• I edited. Does it now answer your question? – Lasse Nov 14 '13 at 19:52
• 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? – Kipras Nov 14 '13 at 19:54
• 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/… – Lasse Nov 14 '13 at 19:56
• @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. – TravisG Nov 17 '13 at 19:02