Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use argument buffers on Metal #4491

Open
kanerogers opened this issue Apr 19, 2023 · 4 comments · May be fixed by gfx-rs/naga#2547
Open

Use argument buffers on Metal #4491

kanerogers opened this issue Apr 19, 2023 · 4 comments · May be fixed by gfx-rs/naga#2547
Labels
area: naga back-end Outputs of naga shader conversion lang: Metal Metal Shading Language naga Shader Translator type: enhancement New feature or request

Comments

@kanerogers
Copy link

kanerogers commented Apr 19, 2023

Background

This is a companion to the wgpu issue #3334

Using argument buffers would simplify the Metal shaders emitted by naga. Instead of having to pass each binding to the shader function as an argument, each binding group can be bundled into a struct which can then be used throughout the shader. For example:

Input wgsl

@group(0) @binding(0) var u_texture : texture_2d<f32>;
@group(0) @binding(1) var u_sampler : sampler;

@fragment
fn frag_main(@location(0) uv : vec2<f32>) -> @location(0) vec4<f32> {
  return textureSample(u_texture, u_sampler, uv);
}

Without argument buffers

fragment frag_mainOutput frag_main(
  frag_mainInput varyings_1 [[stage_in]]
, metal::texture2d<float, metal::access::sample> u_texture [[user(fake0)]]
, metal::sampler u_sampler [[user(fake0)]]
  metal::float4 color = u_texture.sample(u_sampler, uv_1);
  return frag_mainOutput { color };
)

With argument buffers

struct Group_0 {
   metal::texture2d<float, metal::access::sample> u_texture [[id(0)]];
   metal::sampler u_sampler [[id(1)]];
};

fragment frag_mainOutput frag_main(
  frag_mainInput varyings_1 [[stage_in]]
  device Group_0 & group_0 [[buffer(0)]]
) {
  metal::float4 color = group_0.u_texture.sample(group_0.u_sampler, uv_1);
  return frag_mainOutput { color };
}
@teoxoy teoxoy added lang: Metal Metal Shading Language area: naga back-end Outputs of naga shader conversion type: enhancement New feature or request labels Apr 24, 2023
@kanerogers
Copy link
Author

Some updates on this one:

  • I've been struggling to get a solid week blocked off to focus on this issue, so my output has been a little sporadic to date
  • With a hacky, MVP implementation I've been able to get the "hello-cube" example working with wgpu/naga
  • My core struggle has been figuring out how much to keep / how much to throw away with the existing implementation as I'm wary of modifying a large, complex codebase that I'm not familiar with

Some notes (for my reference, or for anyone who wants to pick this up should I get hit by a bus):

  • I am reasonably confident that a 1:1 mapping can be made between a WGPU bind group and an argument buffer. That is, the four kinds of resources defined in the WSGL spec (Uniform Buffers, Storage Buffers, Texture resources and Sampler resources) are all legal members of argument buffers, per section 2.13 of the MSL spec:

Argument buffers extend the basic buffer types to include pointers (buffers), textures, texture buffers, and samplers.

  • With this in mind, I believe it makes more sense to preserve the structure of a bind group on both the wgpu and naga side to more easily populate (on the naga side) and encode (on the wgpu side) the relevant argument buffer.

@teoxoy
Copy link
Member

teoxoy commented Sep 15, 2023

Some considerations:

  • argument buffers require MSL 2 (i.e. iOS 11.0+ macOS 10.13+); the WebGPU spec is ok with this and so is Firefox but this might still impact naga/wgpu users
  • argument buffer tier 1 doesn't support writeable textures (ref)

@kanerogers kanerogers linked a pull request Oct 9, 2023 that will close this issue
@jimblandy
Copy link
Member

@kanerogers The "Without argument buffers" example in the top comment seems mangled - is that valid MSL?

@kanerogers
Copy link
Author

@kanerogers The "Without argument buffers" example in the top comment seems mangled - is that valid MSL?

Ah, good pick up! 😅

No, it's not valid MSL. This was just a rough sketch I wrote, mostly for myself, when I was first looking into the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga back-end Outputs of naga shader conversion lang: Metal Metal Shading Language naga Shader Translator type: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants