Skip to content

Commit 87006a1

Browse files
committed
[metal] Unify entry point to create shader module passthrough
1 parent 263e0fa commit 87006a1

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

tests/gpu-tests/device.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,13 @@ static DEVICE_DESTROY_THEN_MORE: GpuTestConfiguration = GpuTestConfiguration::ne
512512
|| unsafe {
513513
let _ = ctx
514514
.device
515-
.create_shader_module_spirv(&wgpu::ShaderModuleDescriptorSpirV {
516-
label: None,
517-
source: std::borrow::Cow::Borrowed(&[]),
518-
});
515+
.create_shader_module_passthrough(
516+
&wgpu::ShaderModuleDescriptorPassthrough::SpirV(
517+
&wgpu::ShaderModuleDescriptorSpirV {
518+
label: None,
519+
source: std::borrow::Cow::Borrowed(&[]),
520+
})
521+
);
519522
},
520523
Some("device with '' label is invalid"),
521524
);

wgpu/src/api/device.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,35 +138,25 @@ impl Device {
138138
ShaderModule { inner: module }
139139
}
140140

141-
/// Creates a shader module from SPIR-V binary directly.
141+
/// Creates a shader module for passthrough to bypass naga and validations.
142142
///
143143
/// # Safety
144144
///
145-
/// This function passes binary data to the backend as-is and can potentially result in a
146-
/// driver crash or bogus behaviour. No attempt is made to ensure that data is valid SPIR-V.
147-
///
148-
/// See also [`include_spirv_raw!`] and [`util::make_spirv_raw`].
145+
/// This function passes data to the backend as-is and can potentially result in a
146+
/// driver crash or bogus behaviour. No attempt is made to ensure that data is valid.
149147
#[must_use]
150-
pub unsafe fn create_shader_module_spirv(
148+
pub unsafe fn create_shader_module_passthrough(
151149
&self,
152-
desc: &ShaderModuleDescriptorSpirV<'_>,
150+
desc: &ShaderModuleDescriptorPassthrough<'_>,
153151
) -> ShaderModule {
154-
let module = unsafe { self.inner.create_shader_module_spirv(desc) };
155-
ShaderModule { inner: module }
156-
}
157-
158-
/// Creates a shader module from Metal MSL shader directly.
159-
///
160-
/// # Safety
161-
///
162-
/// This function passes the source to the backend as-is and can potentially result in a
163-
/// driver crash or bogus behaviour. No attempt is made to ensure that source code is valid.
164-
#[must_use]
165-
pub unsafe fn create_shader_module_msl(
166-
&self,
167-
desc: &ShaderModuleDescriptorMsl<'_>,
168-
) -> ShaderModule {
169-
let module = unsafe { self.inner.create_shader_module_msl(desc) };
152+
let module = match desc {
153+
ShaderModuleDescriptorPassthrough::SpirV(desc) => {
154+
unsafe { self.inner.create_shader_module_spirv(desc) }
155+
},
156+
ShaderModuleDescriptorPassthrough::Msl(desc) => {
157+
unsafe { self.inner.create_shader_module_msl(desc) }
158+
},
159+
};
170160
ShaderModule { inner: module }
171161
}
172162

wgpu/src/api/shader_module.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,19 @@ pub struct ShaderModuleDescriptor<'a> {
223223
}
224224
static_assertions::assert_impl_all!(ShaderModuleDescriptor<'_>: Send, Sync);
225225

226-
/// Descriptor for a shader module given by SPIR-V binary, for use with
227-
/// [`Device::create_shader_module_spirv`].
226+
/// Descriptor for a pre-compiled shader module passthrough, for use with
227+
/// [`Device::create_shader_module_passthrough`].
228+
///
229+
/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
230+
/// only WGSL source code strings are accepted.
231+
pub enum ShaderModuleDescriptorPassthrough<'a> {
232+
/// Passthrough for SPIR-V binaries.
233+
SpirV(&'a ShaderModuleDescriptorSpirV<'a>),
234+
/// Passthrough for MSL source code.
235+
Msl(&'a ShaderModuleDescriptorMsl<'a>),
236+
}
237+
238+
/// Descriptor for a shader module given by SPIR-V binary.
228239
///
229240
/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
230241
/// only WGSL source code strings are accepted.
@@ -237,8 +248,7 @@ pub struct ShaderModuleDescriptorSpirV<'a> {
237248
}
238249
static_assertions::assert_impl_all!(ShaderModuleDescriptorSpirV<'_>: Send, Sync);
239250

240-
/// Descriptor for a shader module given by Metal MSL source, for use with
241-
/// [`Device::create_shader_module_msl`].
251+
/// Descriptor for a shader module given by Metal MSL source.
242252
///
243253
/// This type is unique to the Rust API of `wgpu`. In the WebGPU specification,
244254
/// only WGSL source code strings are accepted.

0 commit comments

Comments
 (0)