wgpu/api/
shader_module.rs

1use core::{future::Future, marker::PhantomData};
2pub use wgt::{CompilationInfo, CompilationMessage, CompilationMessageType, SourceLocation};
3
4use crate::*;
5
6/// Handle to a compiled shader module.
7///
8/// A `ShaderModule` represents a compiled shader module on the GPU. It can be created by passing
9/// source code to [`Device::create_shader_module`]. MSL shader or SPIR-V binary can also be passed
10/// directly using [`Device::create_shader_module_passthrough`]. Shader modules are used to define
11/// programmable stages of a pipeline.
12///
13/// Corresponds to [WebGPU `GPUShaderModule`](https://gpuweb.github.io/gpuweb/#shader-module).
14#[derive(Debug, Clone)]
15pub struct ShaderModule {
16    pub(crate) inner: dispatch::DispatchShaderModule,
17}
18#[cfg(send_sync)]
19static_assertions::assert_impl_all!(ShaderModule: Send, Sync);
20
21crate::cmp::impl_eq_ord_hash_proxy!(ShaderModule => .inner);
22
23impl ShaderModule {
24    /// Get the compilation info for the shader module.
25    pub fn get_compilation_info(&self) -> impl Future<Output = CompilationInfo> + WasmNotSend {
26        self.inner.get_compilation_info()
27    }
28
29    #[cfg(custom)]
30    /// Returns custom implementation of ShaderModule (if custom backend and is internally T)
31    pub fn as_custom<T: custom::ShaderModuleInterface>(&self) -> Option<&T> {
32        self.inner.as_custom()
33    }
34}
35
36/// Source of a shader module.
37///
38/// The source will be parsed and validated.
39///
40/// Any necessary shader translation (e.g. from WGSL to SPIR-V or vice versa)
41/// will be done internally by wgpu.
42///
43/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
44/// only WGSL source code strings are accepted.
45#[cfg_attr(feature = "naga-ir", expect(clippy::large_enum_variant))]
46#[derive(Clone, Debug)]
47#[non_exhaustive]
48pub enum ShaderSource<'a> {
49    /// SPIR-V module represented as a slice of words.
50    ///
51    /// See also: [`util::make_spirv`], [`include_spirv`]
52    #[cfg(feature = "spirv")]
53    SpirV(alloc::borrow::Cow<'a, [u32]>),
54    /// GLSL module as a string slice.
55    ///
56    /// Note: GLSL is not yet fully supported and must be a specific ShaderStage.
57    #[cfg(feature = "glsl")]
58    Glsl {
59        /// The source code of the shader.
60        shader: alloc::borrow::Cow<'a, str>,
61        /// The shader stage that the shader targets. For example, `naga::ShaderStage::Vertex`
62        stage: naga::ShaderStage,
63        /// Key-value pairs to represent defines sent to the glsl preprocessor.
64        ///
65        /// If the same name is defined multiple times, the last value is used.
66        defines: &'a [(&'a str, &'a str)],
67    },
68    /// WGSL module as a string slice.
69    #[cfg(feature = "wgsl")]
70    Wgsl(alloc::borrow::Cow<'a, str>),
71    /// Naga module.
72    #[cfg(feature = "naga-ir")]
73    Naga(alloc::borrow::Cow<'static, naga::Module>),
74    /// Dummy variant because `Naga` doesn't have a lifetime and without enough active features it
75    /// could be the last one active.
76    #[doc(hidden)]
77    Dummy(PhantomData<&'a ()>),
78}
79static_assertions::assert_impl_all!(ShaderSource<'_>: Send, Sync);
80
81/// Descriptor for use with [`Device::create_shader_module`].
82///
83/// Corresponds to [WebGPU `GPUShaderModuleDescriptor`](
84/// https://gpuweb.github.io/gpuweb/#dictdef-gpushadermoduledescriptor).
85#[derive(Clone, Debug)]
86pub struct ShaderModuleDescriptor<'a> {
87    /// Debug label of the shader module. This will show up in graphics debuggers for easy identification.
88    pub label: Label<'a>,
89    /// Source code for the shader.
90    pub source: ShaderSource<'a>,
91}
92static_assertions::assert_impl_all!(ShaderModuleDescriptor<'_>: Send, Sync);
93
94/// Descriptor for a shader module given by any of several sources.
95/// At least one of the shader types that may be used by the backend must be `Some`
96///
97/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
98/// only WGSL source code strings are accepted.
99pub type ShaderModuleDescriptorPassthrough<'a> =
100    wgt::CreateShaderModuleDescriptorPassthrough<'a, Label<'a>>;