gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
11.58k stars 859 forks source link

naga: Input attachments #5233

Open Atilogit opened 5 months ago

Atilogit commented 5 months ago

Is your feature request related to a problem? Please describe. WGSL doesn't have syntax to specify input attachments for Vulkan subpasses. This is limiting when using naga as a shader compiler for Vulkan applications.

Describe the solution you'd like naga could search for expressions like textureLoad(tex, vec2<i32>(floor(coord.xy)), 0) in fragment shaders with the coord parameter being a @builtin(position) as part of the Validator and return the result as part of the ModuleInfo. This would be similar to FunctionInfo::sampling for the GLSL backend and could be used in the SPIR-V (or GLSL) backend to generate code reading from input attachments.

To illustrate, this would be the same as automatically replacing

@group(0) @binding(0) var tex: texture_2d<f32>;

by

// glsl because this doesn't exist in wgsl
layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput tex;

and

textureLoad(tex, vec2<i32>(floor(coord.xy)), 0)

by

// glsl because this doesn't exist in wgsl
subpassLoad(tex)

A check for Capability::InputAttachment from naga::back::spv::Options would be necessary and naga::back::spv::WriterFlags would have to be extended by a flag (LOCAL_LOAD_AS_INPUT_ATTACHMENT for example) which would be disabled by default. The input_attachment_index for each relevant uniform could be passed to the SPIR-V backend as part of naga::back::spv::Options aswell.

I think this is relatively straightforward to implement because the code structure seems to already be in place for something like this.

Describe alternatives you've considered Implementing this outside of naga would require parsing, interpreting, changing and reassembling the generated SPIR-V which seems very redundant and slow. I assume changing WGSL would not be an option since this is specific to Vulkan.

Additional context I'd be happy to have a go at implementing this if my proposal seems reasonable. I'm open for improvements and suggenstions, of course.

teoxoy commented 4 months ago

We are open to PRs adding this sort of features. I'm not too familiar with this feature but I think this would require wgpu changes as well won't it?