wgpu icon indicating copy to clipboard operation
wgpu copied to clipboard

[BREAKING] Wrap `ShaderSource::Naga` in `Cow<'static>`

Open daxpedda opened this issue 3 years ago • 3 comments

Checklist

  • [x] Run cargo clippy.
  • [x] Run RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown if applicable.
  • [ ] Add change to CHANGELOG.md. See simple instructions inside file.

Connections Depends on https://github.com/gfx-rs/naga/pull/2013.

Description I'm currently pre-compiling shaders and storing them as const, but ShaderSource::Naga wants to own naga::Modules, as far as I understand, for good reasons, because it stores them, so not owning them would be problematic. Using Cow<'static, Module>, solves this, even if it looks strange.

Testing None.

daxpedda avatar Jul 21 '22 10:07 daxpedda

Could you provide an example that this change fixes? Is there another way to handle it without changing this type?

grovesNL avatar Aug 07 '22 23:08 grovesNL

// I'm hiding some stuff here, but I've done this with a proc-macro
// and using `serde` to deserialize the `Module` in a `Lazy`.
const MODULE: &'static Module = ...;

// With the PR.
let source = ShaderModuleDescriptor {
    label: None,
    source: ShaderSource::Naga(Cow::Borrowed(MODULE)),
};
// Without the PR.
let source = ShaderModuleDescriptor {
    label: None,
    source: ShaderSource::Naga(MODULE.clone()),
};

// With the PR, I can now keep re-using the `Module` without having to clone it unnecessarily.
device.create_shader_module(source);

I hope this explains it.

~~On a side note, this change would also solve the lifetime issue in #2890.~~ (it wouldn't, because it's 'static)

Is there another way to handle it without changing this type?

Not that I'm aware of, unless we want to carry around a lifetime I guess.

daxpedda avatar Aug 07 '22 23:08 daxpedda

Probably related: #2801.

daxpedda avatar Aug 07 '22 23:08 daxpedda

Now that https://github.com/gfx-rs/naga/pull/2013 was merged, I updated the relevant parts to depend on it.

daxpedda avatar Aug 25 '22 07:08 daxpedda

I think CI isn't failing because of this PR, it fails with the same errors in the latest commit on master too.

daxpedda avatar Aug 25 '22 07:08 daxpedda

Added CHANGELOG entry and rebased, not sure why CI fails now, but it doesn't look like it's because of this PR.

daxpedda avatar Aug 29 '22 07:08 daxpedda

Rebased after #3008.

daxpedda avatar Sep 03 '22 13:09 daxpedda