Recursive AI

Set HLSL point size for Vulkan point sprites

OneShader is a free, open-source HLSL compiler library for .NET from Recursive AI. MIT or Apache-2.0.

Write a point’s width in pixels from the vertex shader. For points larger than one pixel, Vulkan needs the largePoints device feature.

Terminal
dotnet add package RecursiveAI.OneShader

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

HLSL
// Vertex output member in Points.hlsl
[[vk::builtin("PointSize")]]
float Size : PSIZE;

Set Size for every vertex. The sample uses 4 and 12 pixels and compiles to points.spv.

The example is the full program below: unzip it and dotnet run.

Full program

Save this input as Points.hlsl beside Program.cs, or use the copy in the zip.

HLSL
struct Point
{
    float4 Position : SV_Position;
    [[vk::builtin("PointSize")]]
    float Size : PSIZE;
};
 
[shader("vertex")]
Point VSMain(uint vertex : SV_VertexID)
{
    Point point;
    point.Position = float4(vertex == 0 ? -0.5 : 0.5,
                            0, 0, 1);
    point.Size = vertex == 0 ? 4.0 : 12.0;
    return point;
}
C#
using OneShader;
 
var result = ShaderCompiler.CompileFile("Points.hlsl",
    new CompileOptions { Targets = [CompileTarget.SpirV] });
if (!result.Success)
    throw new InvalidOperationException(
        string.Join(Environment.NewLine, result.Diagnostics));
 
var shader = result.Find("VSMain", CompileTarget.SpirV)!;
File.WriteAllBytes("points.spv", shader.ToArray());
Console.WriteLine("Wrote points.spv");
foreach (var output in result.Reflection!.EntryPoints.Single().Outputs)
    Console.WriteLine($"Vertex output: {output.BuiltIn}");
Text
Wrote points.spv
Vertex output: Position
Vertex output: PointSize

The attribute must be on a scalar float vertex output. On an input, a non-float, or another shader stage, OneShader reports OSH0340.

Variations

Draw one-pixel points without largePoints. In VSMain, replace the assignment to point.Size with this line; keep the built-in declaration.

HLSL
point.Size = 1.0;

The short answer

Put [[vk::builtin("PointSize")]] on a float vertex output and write the size in pixels. OneShader 1.7.3 emits the Vulkan PointSize built-in, not a user-defined location. Use point-list topology and enable largePoints before drawing the 4- and 12-pixel points in the example. The C# program compiles the vertex stage; use it with your own graphics pipeline and fragment shader to draw.

Why does Vulkan need PointSize in the vertex shader?

For a point-list pipeline without a geometry or tessellation evaluation shader, the last vertex stage needs to write PointSize. SV_Position says where the point goes; it doesn’t say how wide it is. PSIZE is the HLSL semantic in the sample, but the vk::builtin attribute supplies the SPIR-V mapping. The output has no Location, so don’t reserve a user varying slot for it.

Set the pipeline input assembly to VK_PRIMITIVE_TOPOLOGY_POINT_LIST. The example reads SV_VertexID instead of a vertex buffer: a draw of two vertices starting at zero supplies the two positions.

To texture the point, use the fragment shader’s PointCoord as the texture coordinates. The Vulkan Samples particle recipe shows this in GLSL: it samples the sprite texture at gl_PointCoord. OneShader 1.7.3 can compile the HLSL vertex stage above, but it doesn’t expose PointCoord as a fragment input.

How do you check whether Vulkan can draw large points?

Query VkPhysicalDeviceFeatures.largePoints and enable it when creating the logical device. Check pointSizeRange and pointSizeGranularity for the device’s supported sizes and spacing. For a quick look on Linux with Vulkan Tools installed, run:

Terminal
vulkaninfo | grep -A2 -E 'deviceName|pointSizeRange:|largePoints'

On a GeForce RTX 4070, the query reported largePoints = true and a pointSizeRange of 1 to 2047.94 pixels. That indicates support, not that the feature has already been enabled in your app. If it’s unavailable, use size 1.0 or render each sprite as two triangles instead. The SPIR-V built-in itself adds no capability, so seeing PointSize in the compiler’s output is not a substitute for checking the device.

All oneshader recipes