r/GraphicsProgramming • • 2d 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.

10 Upvotes

7 comments sorted by

•

u/AutoModerator 2d ago

Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:

  • No showcases.
  • Disclose AI usage in post or project.
  • Present a topic to talk about, e.g. how something works.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

6

u/OkAccident9994 2d ago

In your test you upload 10 million 2D points to your 5080, that is ~80 megabytes. Then you draw them. The entire thing takes 84 milliseconds, so you are pushing data to your gpu at 0.1% of its bandwidth.

That is impressive that you manage to draw points this slowly.

-6

u/Euphoric-Life-848 2d ago

The GPU is innocent here. Those 10M points never reach it.

Each update, one CPU core walks all 10M points and keeps 4 per pixel column (first, last, min, max). Only a few thousand points get uploaded. The 84 ms is that scan: no SIMD, no threads. In return, a redraw costs 1.2 ms at any size.

So yes, the scan is slow, and it's next on my list. If you'd rather throw all 10M at the GPU every frame, show me how you'd make that win.

2

u/CradleZero 2d ago

That per-pixel ownership rule for translucent joints is clever. Out of curiosity, did you try max-alpha blending or depth-testing by segment index before writing the neighbor check in the fragment shader?

1

u/Euphoric-Life-848 2d ago

nope, didn't try either, went straight to the neighbour check.

max blend only fixes the overlap inside the line itself. it doesn't blend over the grid or other series behind it, so i'd need an offscreen target per transparent line. didn't want that.

depth by index = whoever draws first wins the pixel. at a joint the first segment's soft edge grabs pixels the next segment should fully cover and you get little notches. nearest segment wins keeps the AA clean and needs no extra buffer.

it's not perfect btw. it only checks 2 segments each side, so if the line crosses itself later it still double blends there.

1

u/CradleZero 2d ago

That makes a lot of sense, skipping an offscreen target per transparent line is worth the self-crossing trade-off. Appreciate the breakdown on the soft-edge notches too.