How to emulate coverage to alpha in vulkan

Hello,

I am porting an existing OpenGL 1.3 2D renderer to vulkan. This renderer uses GL_POLYGON_SMOOTH and GL_LINE_SMOOTH extensively.

I am looking for a way to compute the (approximate) pixel coverage of a fragment.
My current approach involves rendering into a multisampled image with VK_SAMPLE_COUNT_16_BIT, and using gl_SampleMaskIn in the fragment shader to compute the final alpha value:

float alpha = popcount(gl_SampleMaskIn[0]) * (1.0 / 16.0);
gl_SampleMask[0] = 0xFFFFFFFF;

This seems to work ok, but it’s very wasteful - i don’t actually need a multisampled image, I just need the rasterizer to compute the sample mask for me.

Things i already looked into:

  • VK_EXT_multisampled_render_to_single_sampled - not very widely supported.
  • Using gl_SampleMask[0] = 1 and VK_RESOLVE_MODE_SAMPLE_ZERO_BIT. This requires integer formats, and there’s no integer format that supports color_attachment (at least on my setup).
  • Gemini claimed that one could set rasterizationSamples = VK_SAMPLE_COUNT_16_BIT in the pipeline creation and then render into a non-multisampled image anyway, but validation layers disagree (and i got device_lost anyway when i tried it).
  • Some discussion in “Functional equivalent to GL_LINE_SMOOTH?” on this forum, but doesn’t cover POLYGON_SMOOTH

I know that multisampling is strictly better than coverage-to-alpha, but i am looking to match the existing opengl implementation as much as I can.

I’m wondering whether a screen-space coverage approximation might be worth considering here, especially since this is a 2D renderer.

For some simple shapes, estimating coverage analytically in the fragment shader could avoid the need for a 16-sample attachment altogether.

Of course, that gets less straightforward for arbitrary polygons and overlapping edges, so I’m not sure it would fit your existing renderer without substantial changes.

Is matching the old OpenGL output pixel-for-pixel important, or would a visually similar result be acceptable?

That distinction might make a big difference in which approach is worth pursuing.