Skip to content

Nalgebra vector fails to be recongized #247

Open
@marstaik

Description

@marstaik

I tried using nalgebra (no-std) for types in shaders, but the simplest shaders fails to compile.

use nalgebra::Vector4;
use spirv_std::spirv;

#[allow(dead_code)]
#[spirv(fragment)]
pub fn main_fs(output: &mut Vector4<f32>) {
	*output = Vector4::new(1.0, 0.0, 0.0, 1.0);
}

#[allow(dead_code)]
#[spirv(vertex)]
pub fn main_vs(
	#[spirv(vertex_index)] vert_id: i32,
	#[spirv(position, invariant)] out_pos: &mut Vector4<f32>,
) {
	*out_pos = Vector4::new(
		(vert_id - 1) as f32,
		((vert_id & 1) * 2 - 1) as f32,
		0.0,
		1.0,
	);
}

Is the module I am trying to compile, and the error received is:

  error: [VUID-Position-Position-04321] According to the Vulkan spec BuiltIn Position variable needs to be a 4-component 32-bit float vector. ID <3> (OpVariable) is not a float vector.
    |
    = note: module `/home/marios/proj/hestia_vk/target/spirv-builder/spirv-unknown-vulkan1.2/release/deps/shaders.spvs/mesh-main_vs.spv`

  warning: an unknown error occurred
    |
    = note: spirv-opt failed, leaving as unoptimized
    = note: module `/home/marios/proj/hestia_vk/target/spirv-builder/spirv-unknown-vulkan1.2/release/deps/shaders.spvs/mesh-main_vs.spv`

  error: error:0:0 - [VUID-Position-Position-04321] According to the Vulkan spec BuiltIn Position variable needs to be a 4-component 32-bit float vector. ID <3> (OpVariable) is not a float vector.
           %50 = OpInBoundsAccessChain %_ptr_Output__arr_float_uint_4 %out_pos %uint_0 %uint_0 %uint_0
    |
    = note: spirv-val failed

If I comment out the main_vs function all is well.

It seems like the Vector4 type isnt being interpreted as a [f32;4]

I am wondering if this is because of the underlying type: pub struct ArrayStorage<T, const R: usize, const C: usize>(pub [[T; R]; C]);

It evaluates with R=4, C=1, making the inner type [[f32; 4],1]

Activity

Turtyo

Turtyo commented on May 6, 2025

@Turtyo

Could it because it's a wrapper ? Have you tried defining a struct with just a [f32; 4] inside and see if that works ?
Might just also be that it's indeed a [[f32; 4], 1] and it doesn't cast to [f32; 4] directly

marstaik

marstaik commented on May 9, 2025

@marstaik
Author

Strangely, both [f32;4] and [[f32;4];1] don't work. The shader compilation seems to fail silently.

LegNeato

LegNeato commented on May 11, 2025

@LegNeato
Collaborator

I believe for inputs and outputs it is hardcoded to needglam. Glam also uses spirv intrinsics on vulkan so it will be faster currently.

We've had discussions on the best way to support any implementation (mint, features, a trait, etc). We haven't landed on one yet but I personally believe it is important to be able to bring your own library in this space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @LegNeato@marstaik@Turtyo

        Issue actions

          Nalgebra vector fails to be recongized · Issue #247 · Rust-GPU/rust-gpu