What is a fragment?

I was looking at the Optimizing Graphics Performance http://docs.unity3d.com/Documentation/Manual/OptimizingGraphicsPerformance.html

and it said,“Choose to use less textures per fragment.”

So what is a fragment and how can I choose to use less of them?

Thanks for all the help in advance!

So it’s referring to the fragment program in the shader. Shaders (at least in Unity) have basically two things that happen.

  1. Every model vertex is run through the vertex program of the chosen shader. This program outputs data that will be consumed per pixel (or fragment) that will actually need to be drawn to the screen. The vertex program provides these outputs per vertex (including the “projection position” of the vertex) and the GPU “interpolates” them for each fragment to be drawn on the screen.

The practical upshot of this is that you configure models by putting things on vertices (like UV coordinates, positions, normals etc) and then the GPU handles making up the faces by working out what values should be used for a particular point on a particular triangle. This is what it passes on to the fragment program.

  1. Every fragment between the vertices that is considered visible is coloured by the fragment program. Basically this gets those interpolated vertex outputs and decides what colour the pixel should be.

So for example the fragment program gets passed a coordinate from a texture based on the UVs at the three points that make up the triangle that this pixel is part of, it gets passed any other information that the shader author needs to colour the final pixel.

So if you use basic “Diffuse” there is one texture to be read for the model fragments. That texture is read, the pixel is lit based on the appropriate lights (the number and method is down to the shader) and then the colour is output and that is what is written to the display.

If you use a bumped specular shader then it’s got a lot more textures to read from. If you use a reflective shader then that’s a bunch of complex lookups too.

The lightest weight shader would be one that used no texture at all! Perhaps using colours set at the vertices to decide on what colour should be supplied to the lighting calculation.

So if you want to reduce the number of textures per fragment, choose shaders that only have 1 texture input - like Diffuse.

For improved performance you can also choose vertex lit shaders because they do the lighting calculation per vertex rather than per pixel, or of course you could have totally unlit textures (which are normally used for 2D graphics, but I saw a great game in 3D using them recently).

Vertex lit shaders are useless if you are using lots of dynamic point or spot lights (though Lightmapping can fix this if your lights and lit objects are static).