Recursive AI

Bind a fixed-size HLSL texture array in C#

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

Write two image views to one binding. The zip needs a Vulkan 1.2 driver.

Terminal
dotnet add package RecursiveAI.OneShader.Build
dotnet add package Silk.NET.Vulkan

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

C#
using static Example.Shaders.Layers.Bindings;
using var host = new TextureHost(Tex.Count);
host.WriteTextures(Tex.Set, Tex.Binding);

Unzip the example, then dotnet run.

Writes two sampled-image descriptors at set 0, binding 0. The snippet uses TextureHost.cs from the zip and Layers.Bindings.Tex, which OneShader.Build generates from the Layers.hlsl below. Keep both files next to the project. Add these lines in the project’s <PropertyGroup>. The Build package compiles .hlsl files in the project by default.

XML
<RootNamespace>Example</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

Layers.hlsl (in the zip):

HLSL
Texture2D<float4> Tex[2] : register(t0);
 
[shader("pixel")]
float4 Main() : SV_Target
{
    return Tex[0].Load(int3(0, 0, 0))
         + Tex[1].Load(int3(0, 0, 0));
}

The full program

TextureHost.cs creates two 2D image views, one per layer of a tiny image, and allocates a descriptor set. It writes both views to successive elements of the same binding with UpdateDescriptorSets. The example stops at the descriptor update; it doesn’t upload pixels or draw.

The snippet calls the WriteTextures(setNumber, bindingNumber) overload, which builds the layout binding; the full program calls the overload that takes the layout binding directly.

C#
using Silk.NET.Vulkan;
using Tex = Example.Shaders.Layers.Bindings.Tex;
 
using var host = new TextureHost(Tex.Count);
var layout = new DescriptorSetLayoutBinding
{
    Binding = Tex.Binding,
    DescriptorType = DescriptorType.SampledImage,
    DescriptorCount = Tex.Count,
    StageFlags = ShaderStageFlags.FragmentBit
};
host.WriteTextures(Tex.Set, layout);
Console.WriteLine($"Set {Tex.Set}, binding {layout.Binding}: " +
    $"{layout.DescriptorCount} sampled images written");
Text
Set 0, binding 0: 2 sampled images written

Here’s the descriptor write in TextureHost.cs. infos holds the two image views in array order.

C#
fixed (DescriptorImageInfo* images = infos)
{
    var write = new WriteDescriptorSet
    {
        SType = StructureType.WriteDescriptorSet,
        DstSet = set, DstBinding = binding.Binding, DstArrayElement = 0,
        DescriptorCount = binding.DescriptorCount,
        DescriptorType = binding.DescriptorType, PImageInfo = images
    };
    vk.UpdateDescriptorSets(device, 1, &write, 0, null);
}

If the project can’t find Layers.Bindings.Tex, check that Layers.hlsl is included and your namespace matches the project’s RootNamespace.

Variations

Reserve four slots. Replace the declaration and rerun. The host reads the new generated Count and writes four descriptors without a C# edit.

HLSL
Texture2D<float4> Tex[4] : register(t0);
Text
Set 0, binding 0: 4 sampled images written

The short answer

Use Set and Binding from the generated C# class to locate the Vulkan binding. Use Count for the descriptor count in the layout and for the number of image views to write at that binding. The number of array elements does not change the number of bindings.

Is Texture2D Tex[2] the same as Texture2DArray?

No. An HLSL resource array such as Texture2D Tex[2] selects a texture descriptor with Tex[i]. A Texture2DArray is one texture resource whose operations take a layer index as part of the location. This example uses two individual 2D views, even though the views happen to come from two layers of the same image. The shader chooses between descriptors, not between layers of one view.

What does DstArrayElement select in a Vulkan descriptor write?

DstBinding selects the binding in the set. DstArrayElement selects the first element to update inside that binding. The Vulkan descriptor array guide shows two approaches: update all elements in one write or start each write at its own element index. The example uses one write starting at 0. Its PImageInfo array supplies the images in order: the first view is Tex[0], the second is Tex[1].

DescriptorCount in the layout reserves slots. DescriptorCount in a write says how many descriptors that write provides. If you update only the second view later, start at element 1, set the write count to 1, and leave the layout count at 2.

Do fixed-size HLSL texture arrays need descriptor indexing?

Not for the literal indices in this shader. A fixed-size array is not a variable-count or bindless binding. If you change the shader to choose the texture at run time, check the generated bindings guide for the required Vulkan features. In particular, NonUniformResourceIndex marks an index that can differ between shader invocations; that has different feature requirements from a literal array index. Before drawing, also make the images ready for reads and transition them to the layout specified in the descriptors.

All oneshader recipes