<p>It should be reasonably easy to implement a line segment based spline shader using just a vertex shader and fragment shader. The biggest issues are how the splines are represented, getting that data to the GPU, then writing a vertex shader that accesses the correct spline info.</p>
<p>As one possibility:</p>
<ol>
<li>Upload the spline info to an appropriate buffer.</li>
<li>Setup a graphics pipeline to draw lines.</li>
<li>Setup the vertex shader to take a push constant which holds info about which spline to use and other relevant info.</li>
<li>Then do a draw call with the number of line segments needed for the final spline, along with appropriate push data.</li>
</ol>
<p>Since the pipeline is expecting lines every two vertices represent 1 line segment. The vertex shader can use the vertex index (made available through glsl) to determine which segment to draw. IE 0, 1 are segment 0: 2,3 are segment 1. Combined with the push data it can lookup the spline values, compute the segment vertex data, do the appropriate MVP transform for each end of that segment.</p>
<p>The upside is each spline can have a variable number of segments such as a draw call for a spline with 256 vertices would get 128 segments. The vertex shader can also determine line width which could be varied if the splines are 3D to make them thicker as they move away from the camera.</p>
<p>But the ends of the splines could be tricky to get connected smoothly, as the line segments change direction there could be overlap and disjoint looking spots on the splines. (it really depends on the line thickness)</p>
<p>Computing a segment that is equally spaced on splines isn't very easy but is doable.</p>
<p>This answer is making a lot of assumptions about the splines repersentation and what not, if you give more info about where I have gotten it wrong (or areas you just need more info) then let me know and I'll update the answer.</p>

CC BY-SA 4.0
2021-10-19T14:52:32.967Z
2021-10-19T14:52:32.967Z
pmw1234
<p>We have an application which needs to render spline curves (cubic, bezier, b-spline etc.). We currently have working algorithms in C to stroke the control points of these curves into line strips.</p>
<p>The issue we are running to is the need to constantly re-stroke the curves based on zoom and how much of the curve is visible. I am guessing this would be both more practical and more efficient to do on the GPU.</p>
<p>What is the most standard way to approach this problem? I have heard about Geometry shaders but have also heard they are inefficient so I'm not sure if there is maybe a better way. If not, what is the general process of stroking curves like this?</p>
<p>In general, the stroking algorithms take in the control points of the curves, and determine a new set of points for a line strip (based on a tolerance calculated from the zoom level, which defines the stroking interval).</p>
<p>We are using Vulkan if it makes any difference. Thanks!</p>

CC BY-SA 4.0
2021-10-17T18:29:37.293Z
You can draw the curve with shaders on a plane.
joojaa
CC BY-SA 4.0
2023-11-10T00:24:04.443Z
see also https://doi.org/10.1145/3386569.3392392
lhf
<curve><spline>
Rendering splines on GPU
Gary Allen
# Rendering splines on GPU

(Asked by: username_0 on 2021-10-16)

We have an application which needs to render spline curves (cubic, bezier, b-spline etc.). We currently have working algorithms in C to stroke the control points of these curves into line strips.

The issue we are running to is the need to constantly re-stroke the curves based on zoom and how much of the curve is visible. I am guessing this would be both more practical and more efficient to do on the GPU.

What is the most standard way to approach this problem? I have heard about Geometry shaders but have also heard they are inefficient so I'm not sure if there is maybe a better way. If not, what is the general process of stroking curves like this?

In general, the stroking algorithms take in the control points of the curves, and determine a new set of points for a line strip (based on a tolerance calculated from the zoom level, which defines the stroking interval).

We are using Vulkan if it makes any difference. Thanks!

**Tags:** curve, spline

> **Comments:**
> **username_1**: You can draw the curve with shaders on a plane.  
> **username_2**: see also https://doi.org/10.1145/3386569.3392392  

---

## 1 answers

### Answer by username_3 on 2021-10-19. Score: 0

It should be reasonably easy to implement a line segment based spline shader using just a vertex shader and fragment shader. The biggest issues are how the splines are represented, getting that data to the GPU, then writing a vertex shader that accesses the correct spline info.

As one possibility:

1. Upload the spline info to an appropriate buffer.
2. Setup a graphics pipeline to draw lines.
3. Setup the vertex shader to take a push constant which holds info about which spline to use and other relevant info.
4. Then do a draw call with the number of line segments needed for the final spline, along with appropriate push data.

Since the pipeline is expecting lines every two vertices represent 1 line segment. The vertex shader can use the vertex index (made available through glsl) to determine which segment to draw. IE 0, 1 are segment 0: 2,3 are segment 1. Combined with the push data it can lookup the spline values, compute the segment vertex data, do the appropriate MVP transform for each end of that segment.

The upside is each spline can have a variable number of segments such as a draw call for a spline with 256 vertices would get 128 segments. The vertex shader can also determine line width which could be varied if the splines are 3D to make them thicker as they move away from the camera.

But the ends of the splines could be tricky to get connected smoothly, as the line segments change direction there could be overlap and disjoint looking spots on the splines. (it really depends on the line thickness)

Computing a segment that is equally spaced on splines isn't very easy but is doable.

This answer is making a lot of assumptions about the splines repersentation and what not, if you give more info about where I have gotten it wrong (or areas you just need more info) then let me know and I'll update the answer.


---

