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

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

Merged
merged 2 commits into from
Sep 19, 2022

Conversation

daxpedda
Copy link
Contributor

@daxpedda daxpedda commented Jul 21, 2022

Checklist

  • Run cargo clippy.
  • 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 gfx-rs/naga#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 daxpedda force-pushed the shader-source-naga-cow-static branch 4 times, most recently from 88792c2 to 7246ad7 Compare July 21, 2022 11:32
@daxpedda daxpedda force-pushed the shader-source-naga-cow-static branch 2 times, most recently from 78cbb02 to d100286 Compare August 7, 2022 23:12
@grovesNL
Copy link
Collaborator

grovesNL commented Aug 7, 2022

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

@daxpedda
Copy link
Contributor Author

daxpedda commented Aug 7, 2022

// 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
Copy link
Contributor Author

daxpedda commented Aug 7, 2022

Probably related: #2801.

@daxpedda daxpedda force-pushed the shader-source-naga-cow-static branch from d100286 to 92f4db5 Compare August 25, 2022 07:44
@daxpedda
Copy link
Contributor Author

daxpedda commented Aug 25, 2022

Now that gfx-rs/naga#2013 was merged, I updated the relevant parts to depend on it.

@daxpedda daxpedda marked this pull request as ready for review August 25, 2022 07:45
@daxpedda
Copy link
Contributor Author

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

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm game, small comment

wgpu/src/backend/direct.rs Outdated Show resolved Hide resolved
@daxpedda
Copy link
Contributor Author

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

@daxpedda daxpedda force-pushed the shader-source-naga-cow-static branch from deed644 to 7b1abf8 Compare September 3, 2022 13:42
@daxpedda
Copy link
Contributor Author

daxpedda commented Sep 3, 2022

Rebased after #3008.

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully this doesn't start another cowpocolypse 🐄 😆

@cwfitzgerald cwfitzgerald merged commit 95408cd into gfx-rs:master Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants