Skip to content

Shader validation enum#17824

Merged
alice-i-cecile merged 31 commits intobevyengine:mainfrom
fjkorf:shader-validation-enum
Feb 20, 2025
Merged

Shader validation enum#17824
alice-i-cecile merged 31 commits intobevyengine:mainfrom
fjkorf:shader-validation-enum

Conversation

@fjkorf
Copy link
Copy Markdown
Contributor

@fjkorf fjkorf commented Feb 12, 2025

Objective

Make checked vs unchecked shaders configurable
Fixes #17786

Solution

Added ValidateShaders enum to Shader and added create_and_validate_shader_module to RenderDevice

Testing

I tested the shader examples locally and they all worked. I'd like to write a few tests to verify but am unsure how to start.

@fjkorf
Copy link
Copy Markdown
Contributor Author

fjkorf commented Feb 12, 2025

Does this effectively expose this functionality to the user?

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 12, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Feb 12, 2025
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this :) Definitely the right direction! I've highlighted a few small cleanup items, some docs to write, and then we can bubble up the unsafe to the end user who's actually calling these things.

Shouldn't be too bad, and feel free to ask questions or for advice on the tricky bits.

@fjkorf
Copy link
Copy Markdown
Contributor Author

fjkorf commented Feb 12, 2025

not sure if I got the unsafe bit in correctly. I'm getting a warning about it

@fjkorf
Copy link
Copy Markdown
Contributor Author

fjkorf commented Feb 13, 2025

Thanks!

//
// This function passes binary data to the backend as-is and can potentially result in a
// driver crash or bogus behavior. No attempt is made to ensure that data is valid SPIR-V.
unsafe { self.create_shader_module(desc) },
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

having an unsafe function to create unchecked shaders, and having a safe function that also creates unchecked shaders in an unsafe block if you pass in spirv feels wrong (and the comment on the function is misleading).

i think we should probably panic for spirv here.

Comment on lines +76 to +81
// SAFETY:
//
// creates a shader module with user-customizable runtime checks which allows shaders to
// perform operations which can lead to undefined behavior like indexing out of bounds,
// thus it's the caller responsibility to pass a shader which doesn't perform any of this
// operations.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i'd copy the short safety comment below, not much use in copying the long version from the top of the function.

Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

A couple more nits and additions to the docs, but this is nearly ready :) Thanks!

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@fjkorf
Copy link
Copy Markdown
Contributor Author

fjkorf commented Feb 14, 2025

Thank you!

///
/// See [`ValidateShader`](bevy_render::render_resource::ValidateShader) for more information on the tradeoffs involved with shader validation.
#[inline]
pub fn create_and_validate_shader_module(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe I'm missing something, but this looks like it just ends up calling .create_shader_module() and that function doesn't do any validation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This calls create_shader_module in the wgpu device. In that implementation runtime checks are enabled

https://docs.rs/wgpu/latest/src/wgpu/api/device.rs.html#80-85

    pub fn create_shader_module(&self, desc: ShaderModuleDescriptor<'_>) -> ShaderModule {
        let module = self
            .inner
            .create_shader_module(desc, wgt::ShaderRuntimeChecks::checked());
        ShaderModule { inner: module }
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I thought it was calling the bevy one, that makes way more sense.

@IceSentry IceSentry added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 18, 2025
Comment on lines +103 to +110
match &desc.source {
wgpu::ShaderSource::SpirV(_source)
if self
.features()
.contains(wgpu::Features::SPIRV_SHADER_PASSTHROUGH) =>
panic!("no safety checks are performed for spirv shaders. use `create_shader_module` instead"),
_ => self.device.create_shader_module(desc),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what happens if we don't have SPIRV_SHADER_PASSTHROUGH here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we don't have SPRIV_SHADER_PASSTHROGH the match guard will be false. The _ branch will be executed I believe

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok. i'm not sure it has much value here; it will presumably result in failure either way, but a different failure depending on the enabled features. can we just remove it so we always panic on this path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have removed the match guard

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 20, 2025
Merged via the queue into bevyengine:main with commit ed62e59 Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

Make checked vs unchecked shaders configurable

4 participants