Rustgpu how can you index into an array of textures?

I am trying to index into a texture array in rustgpu, I have the following shader code:

#![cfg_attr(target_arch = "spirv", no_std, feature(lang_items))]
#![allow(internal_features)]

extern crate bytemuck;
extern crate spirv_std;

pub use spirv_std::glam::*;
use spirv_std::image::*;
use spirv_std::num_traits::Float;
use spirv_std::spirv;

type Texture2D = Image!(2D, type=f32, sampled);

// Fragment shader
#[spirv(fragment)]
pub fn main_fs(
    in_normal: Vec3,
    in_uv: Vec3,
    in_position: Vec3,
    in_camera_position: Vec3,
    #[spirv(descriptor_set = 1, binding = 1)] image: &[SampledImage<Texture2D>; 69],
    output: &mut Vec4,
)
{
    let index = in_uv.z.round() as usize;
    let albedo = image[index].sample(in_uv.xy());

    *output = albedo;
}

// Vertex shader
pub struct CamUBO
{
    model: Mat4,
    view: Mat4,
    proj: Mat4,
}

#[spirv(vertex)]
pub fn main_vs(
    in_position: Vec3,
    in_normal: Vec3,
    in_uv: Vec3,
    #[spirv(uniform, descriptor_set = 1, binding = 0)] camera_ubo: &CamUBO,

    #[spirv(position, invariant)] screen_pos: &mut Vec4,
    out_normal: &mut Vec3,
    out_uv: &mut Vec3,
    out_position: &mut Vec3,
    out_camera_position: &mut Vec3,
)
{
    *screen_pos = camera_ubo.proj
        * camera_ubo.view
        * camera_ubo.model
        * Vec4::new(in_position.x, in_position.y, in_position.z, 1.0);

    *out_position = screen_pos.xyz();
    *out_normal = (camera_ubo.model * in_normal.extend(0.0)).xyz();
    *out_uv = in_uv;
    *out_camera_position = -camera_ubo.view.col(3).xyz();
}

This fails to compile with the following error:

error: function pointer types are not allowed
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/metadata.rs:94:1
    |
94  | pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: used by `fmt::rt::Argument<'_>`
note: used from within `core::panicking::panic_bounds_check`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: called by `mesh_shader::main_fs`
   --> src/lib.rs:34:18
    |
34  |     let albedo = image[index].sample(in_uv.xy());
    |                  ^^^^^^^^^^^^
note: called by `main_fs`
   --> src/lib.rs:24:8
    |
24  | pub fn main_fs(
    |        ^^^^^^^

error: cannot offset a pointer to an arbitrary element
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: used from within `core::panicking::panic_bounds_check`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: called by `mesh_shader::main_fs`
   --> src/lib.rs:34:18
    |
34  |     let albedo = image[index].sample(in_uv.xy());
    |                  ^^^^^^^^^^^^
note: called by `main_fs`
   --> src/lib.rs:24:8
    |
24  | pub fn main_fs(
    |        ^^^^^^^

error: `u8` without `OpCapability Int8`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:507:1
    |
507 | pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: used by unnamed global (%68396)
note: used from within `core::panicking::panic_bounds_check`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: called by `mesh_shader::main_fs`
   --> src/lib.rs:34:18
    |
34  |     let albedo = image[index].sample(in_uv.xy());
    |                  ^^^^^^^^^^^^
note: called by `main_fs`
   --> src/lib.rs:24:8
    |
24  | pub fn main_fs(
    |        ^^^^^^^

error: cannot cast between pointer types
       from `*struct fmt::rt::Argument<'_> { value: *struct fmt::rt::Opaque {  }, formatter: *fn(*struct fmt::rt::Opaque {  }, *struct fmt::Formatter<'_> { width: struct option::Option<usize> { u32, u32 }, precision: struct option::Option<usize> { u32, u32 }, fill: u32, buf: struct &mut dyn fmt::Write { *struct dyn fmt::Write {  }, *[u32; 3] }, flags: u32, align: u8 }) -> bool }`
         to `*u8`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: used from within `core::panicking::panic_bounds_check`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: called by `mesh_shader::main_fs`
   --> src/lib.rs:34:18
    |
34  |     let albedo = image[index].sample(in_uv.xy());
    |                  ^^^^^^^^^^^^
note: called by `main_fs`
   --> src/lib.rs:24:8
    |
24  | pub fn main_fs(
    |        ^^^^^^^

error: cannot cast between pointer types
       from `*u8`
         to `**fn(*struct fmt::rt::Opaque {  }, *struct fmt::Formatter<'_> { width: struct option::Option<usize> { u32, u32 }, precision: struct option::Option<usize> { u32, u32 }, fill: u32, buf: struct &mut dyn fmt::Write { *struct dyn fmt::Write {  }, *[u32; 3] }, flags: u32, align: u8 }) -> bool`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: used from within `core::panicking::panic_bounds_check`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: called by `mesh_shader::main_fs`
   --> src/lib.rs:34:18
    |
34  |     let albedo = image[index].sample(in_uv.xy());
    |                  ^^^^^^^^^^^^
note: called by `main_fs`
   --> src/lib.rs:24:8
    |
24  | pub fn main_fs(
    |        ^^^^^^^

error: const_bitcast
    |
note: used from within `core::panicking::panic_bounds_check`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: called by `mesh_shader::main_fs`
   --> src/lib.rs:34:18
    |
34  |     let albedo = image[index].sample(in_uv.xy());
    |                  ^^^^^^^^^^^^
note: called by `main_fs`
   --> src/lib.rs:24:8
    |
24  | pub fn main_fs(
    |        ^^^^^^^

error: cannot cast between pointer types
       from `*[struct fmt::rt::Argument<'_> { value: *struct fmt::rt::Opaque {  }, formatter: *fn(*struct fmt::rt::Opaque {  }, *struct fmt::Formatter<'_> { width: struct option::Option<usize> { u32, u32 }, precision: struct option::Option<usize> { u32, u32 }, fill: u32, buf: struct &mut dyn fmt::Write { *struct dyn fmt::Write {  }, *[u32; 3] }, flags: u32, align: u8 }) -> bool }; 2]`
         to `*[struct fmt::rt::Argument<'_> { value: *struct fmt::rt::Opaque {  }, formatter: *fn(*struct fmt::rt::Opaque {  }, *struct fmt::Formatter<'_> { width: struct option::Option<usize> { u32, u32 }, precision: struct option::Option<usize> { u32, u32 }, fill: u32, buf: struct &mut dyn fmt::Write { *struct dyn fmt::Write {  }, *[u32; 3] }, flags: u32, align: u8 }) -> bool }]`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: used from within `core::panicking::panic_bounds_check`
   --> /home/makogan/.rustup/toolchains/nightly-2024-01-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panicking.rs:208:5
    |
208 |     panic!("index out of bounds: the len is {len} but the index is {index}")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: called by `mesh_shader::main_fs`
   --> src/lib.rs:34:18
    |
34  |     let albedo = image[index].sample(in_uv.xy());
    |                  ^^^^^^^^^^^^
note: called by `main_fs`
   --> src/lib.rs:24:8
    |
24  | pub fn main_fs(
    |        ^^^^^^^

warning: `mesh-shader` (lib) generated 3 warnings
error: could not compile `mesh-shader` (lib) due to 7 previous errors; 3 warnings emitted
thread 'main' panicked at crates/vulkan_bindings/src/shader_parsing/rust_gpu_parsing.rs:44:14:
called `Result::unwrap()` on an `Err` value: BuildFailed
stack backtrace:
   0: rust_begin_unwind
             at /rustc/75c68cfd2b9870f2953b62d250bd7d0564a7b56d/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/75c68cfd2b9870f2953b62d250bd7d0564a7b56d/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/75c68cfd2b9870f2953b62d250bd7d0564a7b56d/library/core/src/result.rs:1649:5
   3: vulkan_bindings::shader_parsing::rust_gpu_parsing::parse_shader
   4: vulkan_bindings::shader_program::ShaderProgram::new
   5: <vulkan_bindings::RenderContext as ne_core::gpu_api::GpuApi>::add_shader_from_memory
   6: <vulkan_bindings::RenderContext as ne_core::gpu_api::GpuApi>::add_shader
   7: _02_mesh::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

The most relevant bit is likely error: cannot offset a pointer to an arbitrary element. This seems to be the source of the problem, because when I index the texture array through a constant I do manage to compile.

However, this code is a translation of some hlsl code I had:

[[vk::binding(1, 1)]]
Texture2D<float4> textures[69];
[[vk::binding(2, 1)]]
SamplerState textures_sampler[69];

#include <linalg.hlsl>

vec4 Lambert(
    vec3 position,
    vec3 normal,
    vec3 albedo,
    vec3 light_position,
    vec3 light_color)
{
    vec4 color = vec4(0, 0, 0, 0);
    vec3 l = normalize(light_position - position);

    vec3 n = normalize(normal);

    float intensity = length(light_color);
    light_color = normalize(light_color);
    color = vec4(
        dot(l, n) * albedo * light_color,
        1);

    return color;
}

float4 main(
    [[vk::location(0)]] float3 normal : NORMAL0,
    [[vk::location(1)]] float3 uv : UV0,
    [[vk::location(2)]] float3 position : POSITION0,
    [[vk::location(3)]] float3 camera_position : CAMPOS0
) : SV_TARGET
{
    uint index = int(round(uv.z));
    float4 albedo = textures[index].Sample(textures_sampler[index], uv.xy);

    return (Lambert(
        position,
        normal,
        albedo.xyz,
        vec3(5, 1000, 5),
        normalize(vec3(1, 1, 1))) * 2.0 + 0.1);
}

So in principle it should be possible to generate the appropriate spirv to index into an array of textures.

I was hoping someone could give me a hand

2

Easiest

Apparently the only thing that is needed is to use a special trait in spirv_std toindex into the array without checking the bounds:

#[spirv(fragment)]
pub fn main_fs(
    in_normal: Vec3,
    in_uv: Vec3,
    in_position: Vec3,
    in_camera_position: Vec3,
    #[spirv(descriptor_set = 1, binding = 1)] image: &[SampledImage<Texture2D>; 69],
   &RuntimeArray<SampledTexture2D>,
    output: &mut Vec4,
)
{
    let index = in_uv.z.round() as usize;
    let albedo = unsafe { image.index_unchecked(index).sample(in_uv.xy()) };

    *output = albedo;
}

Old answer

The solution seems to be as follows. First raw arrays are not sutiable for shaders, one must use a special type like this:

#[spirv(fragment)]
pub fn main_fs(
    in_normal: Vec3,
    in_uv: Vec3,
    in_position: Vec3,
    in_camera_position: Vec3,
    #[spirv(descriptor_set = 1, binding = 1)] image: &RuntimeArray<SampledTexture2D>, // this is a slice, almost
    output: &mut Vec4,
)

Then when compiling using spirv builder one needs the following arguments:

        SpirvBuilder::new(path, "spirv-unknown-vulkan1.2")
            .print_metadata(MetadataPrintout::DependencyOnly)
            .preserve_bindings(true)
            .spirv_metadata(SpirvMetadata::NameVariables)
            .scalar_block_layout(true)
            .multimodule(true)
            .capability(Capability::SampledImageArrayDynamicIndexing) // Don't know if we need it
            .capability(Capability::RuntimeDescriptorArray) // Definitely need it
            .extension("SPV_EXT_descriptor_indexing") // Probably need it
            .extra_arg("target-feature=+RuntimeDescriptorArray,") // Might need it
            .build()
            .unwrap()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật