r/GraphicsProgramming • u/Euphoric-Life-848 • 20m ago
Spectra: a C++20 plotting library that renders with Vulkan (2D, 3D, live data)
I want to share how the line rendering works in Spectra, a Vulkan plotting library I'm building, and get feedback on the approach.
Lines as capsules. Every line segment is one screen-space quad, extended by half the line width plus an antialiasing margin. The fragment shader computes the signed distance from the pixel to the segment, which gives round joins and caps for free.
The joint problem. Neighbouring quads overlap around each shared vertex, so a translucent line blends twice there and shows dark beads at every joint. My fix is an ownership rule: each fragment knows the screen positions of the nearby segments, and a pixel is drawn only by the segment nearest to it, with ties going to the lower index. Every draw computes this from the same gl_FragCoord, so there are no seams and no double hits, without a stencil pass or an offscreen target.
Dense lines. A 1600 pixel wide plot has 1600 columns. For a sorted line, each column only needs its first, last, lowest and highest point, and drawing those four gives the same pixels with the spikes intact. With that, a redraw stays at about 1.2 ms from 10k to 10M points on an RTX 5080, and updating a 1M-point line live takes 10.6 ms.
Questions I'd like opinions on: is there a cheaper way to solve the joint double-blend than the per-pixel ownership test, and how do others handle decimation for lines that are not sorted in x?
Code is here if you want to read the shaders: https://github.com/danlil240/Spectra
AI disclosure: much of this project's code was written with AI coding assistants (Claude) under my direction and review, and AI helped me draft this post.
•
u/AutoModerator 20m ago
Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.