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

Precompiled Shaders #3103

Open
dfrg opened this issue Oct 13, 2022 · 4 comments
Open

Precompiled Shaders #3103

dfrg opened this issue Oct 13, 2022 · 4 comments
Labels
area: api Issues related to API surface help required We need community help to make this happen. type: enhancement New feature or request

Comments

@dfrg
Copy link

dfrg commented Oct 13, 2022

I am working with @raphlinus on piet-gpu and we're considering replacing our internal HAL with wgpu-hal but we would like to retain the ability to use precompiled shaders to reduce both binary size and startup cost.

After some investigation, it seems the least invasive solution would be to add variants to ShaderInput for MSL source (or potentially a MTLLibrary instance) and DXIL and feed those through to pipeline creation in the same way that SpirV works for Vulkan today. This would require the user to assign bindings that are compatible with those generated by wgpu-hal during pipeline layout creation. For our purposes, this would be an acceptable constraint.

Alternatively, we've also considered a more sophisticated approach that would allow the user to provide a bind mapping during shader module creation. This seems to require deferring native resource binding until the whole pipeline state is known so that discrepancies between the pipeline layout and the expected binding slots for the entry point can be reconciled.

My understanding is that the project is amenable to such a change, and we're willing to do the work, but wanted to provide an opportunity for other potential users who are interested in this feature to weigh in and develop a consensus on the best path forward.

@cwfitzgerald cwfitzgerald added type: enhancement New feature or request help required We need community help to make this happen. area: api Issues related to API surface labels Oct 22, 2022
@cwfitzgerald
Copy link
Member

cwfitzgerald commented Oct 22, 2022

We definitely would be interested in having this feature!

A couple considerations to keep in mind:

  • In the mid-term future it will be possible to write custom wgpu-hal backends out-of-tree. This is particularly important for console API backends where NDAs are involved, but can also be useful for "trying weird stuff" kind of deals. This means we cannot easily just have enums of choices, as some choices may be unknown to us.
  • I think we should keep the wgpu-authoritative binding model. While specifying the binding model is possible, it is a lot of information to pipe through and is inherently a very leaky abstraction, and suffers from the above problem.

As such I would propose an api that looks something like this:

// Replaces ShaderModuleDescriptorSpirv
pub struct ShaderModuleDescriptorRaw<'a> {
    pub label: Label<'a>,
    /// Raw data of whatever type the backend needs - likely Vec<u32> or a string. Maybe something fancier like a MTLLibrary wrapper.
    pub source: Box<dyn Any>, // Maybe a custom trait?
}

// Replaces create_shader_module_spirv
//
// Note the taking of the descriptor by value.
pub unsafe fn create_shader_module_raw(&self, desc: ShaderModuleDescriptorRaw) -> ShaderModule

Then inside each wgpu-hal backend we can properly document how bindings get mapped based on all possible arguments to a pipeline creation.

@Wandalen
Copy link

@cwfitzgerald, what about the web backend? Does this proposal respect webgl?
The largest obstacle for me is the unreasonably large size of the binary.

@cwfitzgerald
Copy link
Member

cwfitzgerald commented Dec 21, 2022

@Wandalen sure - the webgl/gles backend could accept raw glsl shaders with string as it's type.

@parasyte
Copy link
Contributor

parasyte commented Jan 7, 2023

See also: #1464

According to some basic stats from twiggy (a WASM size profiler), the biggest chunk of code (about 15%) in a small wgpu project is the naga parser and writer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Issues related to API surface help required We need community help to make this happen. type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants