Multisampling
Multisampling is a mechanism to antialias all Vulkan primitives: points, lines, and polygons. The technique is to sample all primitives multiple times at each pixel.
The point is we sample one pixel multiple times, each at the different sample point. Sample count of 1 is ordinary sampling without any anti-aliasing effect. Following is a figure showing various sample count and their sample points distributions.
The color of a pixel is determined by how many sample points are covered by edges of primitives. A classic algorithm of how to determine whether a pixel is inside a polygon is edge function, which is already implemented in hardware. Here are some figures demonstrating how 1 and 2 sample points work:
Sample points inside the triangle are red color dots. Below is what it looks like after drawing pixels, assuming the vertices’ color is dark red.
The more sample dots are going to be, the more granularity in checking whether pixels are inside the triangle.
Colors of partially covered pixels are determined by how many sample points are covered.
New Scene
We create a new scene to demonstrate the feature and it is nothing special when compared to other scenes(EmptyScene
/EarthScene
).
New Render Pass
The render pass has three attachments:
- Color attachment:
colorAttachment.samples
is the sample count other thanVK_SAMPLE_COUNT_1_BIT
.- After resolving the image, we do not need to preserve its content and hence, use
VK_ATTACHMENT_STORE_OP_DONT_CARE
forcolorAttachment.storeOp
here. - The image is not the image to be presented so
colorAttachment.finalLayout
is notVK_IMAGE_LAYOUT_PRESENT_SRC_KHR
butVK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
.
2. Depth attachment:
depthAttachment.samples
is identical tocolorAttachment.samples
.
3. Resolve attachment:
- Used for resolving the multi-sampled image to a non-multisample image so
resolveAttachment.sample
s isVK_SAMPLE_COUNT_1_BIT
. resolveAttachment.loadOp
is meaningless since we will resolve to it and never render to it. For this reason,VK_ATTACHMENT_STORE_OP_DONT_CARE
is used.- The image is the image to be presented so
resolveAttachment.finalLayout
isVK_IMAGE_LAYOUT_PRESENT_SRC_KHR
.
Scene Renderer
Some members worth noting:
_msaaImage
/_msaaView
/_msaaImageMemory
: A temporary image for multisampling.- Lighting:
alignas
is a must to conform to the spec.
Request VkSampleCountFlagBits
to be VK_SAMPLE_COUNT_4_BIT
which will be used as a part of MSAARenderPass
creation.
BuildMSAAImage
is similar to BuildDepthImage
and it creates a temporary image for drawing the scene with multisampling. VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT
is as an optimization that this image will only be used as a transient render target and the content will not be written to memory. samples
is also applied to _depthImage
for correct depth test. And, multisample.rasterizationSamples
is assigned to samples
as well.
Put _msaaView
in attachments
and note image views in the array must accord with the order in both VkAttachmentDescription attachments;
and MSAARenderPass::CreateRenderPassImpl
.