---
title: "Bind a fixed-size HLSL texture array in C#"
url: https://recursiveai.org/oneshader/cookbook/bind-fixed-size-hlsl-texture-array/
description: "Bind a fixed-size HLSL texture resource array in C# with OneShader. Use the generated binding number and count to write a Vulkan descriptor array."
version: "1.7.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneShader.Build) · [GitHub](https://github.com/recursive-ai-oss/oneshader-csharp) · [Docs](https://recursiveai.org/oneshader/docs/) · [Download example (.zip)](https://recursiveai.org/oneshader/cookbook/bind-fixed-size-hlsl-texture-array/bind-fixed-size-hlsl-texture-array.zip)

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

```shell
dotnet add package RecursiveAI.OneShader.Build
dotnet add package Silk.NET.Vulkan
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

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

Unzip [the example](https://recursiveai.org/oneshader/cookbook/bind-fixed-size-hlsl-texture-array/bind-fixed-size-hlsl-texture-array.zip), 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.

```csharp
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.

```csharp
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
```

## Related recipes

- [Generate C# bindings from HLSL](https://recursiveai.org/oneshader/cookbook/generate-csharp-shader-bindings/)
- [Use bindless textures in HLSL](https://recursiveai.org/oneshader/cookbook/bindless-textures/)

---

## 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](https://learn.microsoft.com/en-us/windows/win32/direct3d12/resource-binding-in-hlsl) 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](https://docs.vulkan.org/guide/latest/descriptor_arrays.html) 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](https://recursiveai.org/oneshader/docs/generated-bindings/#resource-arrays) 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.
