Closed Makogan closed 6 months ago
I am not well versed in the internals of rustgpu nor with spirv. But perhaps it would be possible to add an attribute macro to guide the compiler to compile slices of image types into SampledImageArrayDynamicIndexing
, it seems that spirv builder already defines enumerator types that match this case. So i assume it;s just a matter of specifying how and when to turn certain slices into the corresponding dynamic indexing type?
https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_capability
This feels like RuntimeArray
, i.e. &RuntimeArray<SampledImage<Texture2D>>
.
This feels like
RuntimeArray
, i.e.&RuntimeArray<SampledImage<Texture2D>>
.
This is the correct answer, thank you and sorry I missed it.
A different alternative if one uses the old way of doing things, with fixed array sizes is to use unchecked indexing, like this:
// Fragment shader
#[spirv(fragment)]
pub fn main_fs(
in_normal: Vec3,
in_uv: Vec3,
in_position: Vec3,
#[spirv(descriptor_set = 1, binding = 1)] image: &[SampledTexture2D; 69],
// use this for boundless arrays
// #[spirv(descriptor_set = 1, binding = 1)] image: &RuntimeArray<SampledTexture2D>,
output: &mut Vec4,
)
{
// let albedo = unsafe { image.index(index).sample(in_uv.xy()) };
let index = in_uv.z.round() as usize;
let albedo = unsafe { image.index_unchecked(index).sample(in_uv.xy()) };
*output = lambert(
in_position,
in_normal,
albedo.xyz(),
Vec3::new(5., 1000., 5.),
Vec3::new(1., 1., 1.).normalize() * 1.5 + Vec3::splat(0.5),
);
}
I am not entirely sure whether to flag this as a bug or as missing functionality. Consider this HLSL example which I have tested by compiling it to spir-v and running it:
I was trying to port this code onto rust and compiling it through rustgpu, as in this example:
This creates a long list of errors, but the most pertinent I think is this:
error: cannot offset a pointer to an arbitrary element
And indeed, setting the index to a constant does compile and run fine. Given that the HLSL code is able to compile down into spirv, it should be possible to generate the correct assembly to index into the texture array. I am unsure if there might be an existing work around however.
Example & Steps To Reproduce
Run spirv-builder and compile the above snippet.