How Does Multisampling Work in OpenGL Rendering Pipeline
After doing some reading and online search, I finally got the idea how multisampling works in opengl pipeline.
The idea is, every primitive is sampled multiple times per pixel. When it comes to present the final image, all of the samples for the pixel are resolved to determine the final pixel’s color.
In the pipeline, multisampling works as follows:
1. The rasterizer generates separate depth and stencil values for samples per pixel.
2. When calculating the colors of the samples, the pipeline invokes the fragment program if any of the samples are located within the primitive. In the fragment program, the color is calculated using the sample located in the center of the pixel. Now we get a single color from the fragment program, this color is coped to all the samples within the primitive.
3. The colors of the samples are stored in a multisampled texture. Each sample may cover multiple primitives, but the final multsampled texture only store the nearest one. Thus, when updating an existed sample, depth test is excuted to determine whether to update.
4. Final stage: average the multisamped texture to generate a single color corresponding to its pixel.