Skip to content

Commit 109b8c9

Browse files
committed
target enum: make SpirvBuilder::new return Result
1 parent 57def97 commit 109b8c9

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

crates/spirv-builder/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,15 @@ impl Default for SpirvBuilder {
492492
}
493493

494494
impl SpirvBuilder {
495-
pub fn new(path_to_crate: impl AsRef<Path>, target: impl IntoSpirvTarget) -> Self {
496-
Self {
495+
pub fn new(
496+
path_to_crate: impl AsRef<Path>,
497+
target: impl IntoSpirvTarget,
498+
) -> Result<Self, SpirvTargetParseError> {
499+
Ok(Self {
497500
path_to_crate: Some(path_to_crate.as_ref().to_owned()),
498-
target: target.to_spirv_target_env().ok(),
501+
target: Some(target.to_spirv_target_env()?),
499502
..SpirvBuilder::default()
500-
}
503+
})
501504
}
502505

503506
/// Sets the path of the "target specification" file.

examples/multibuilder/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use spirv_builder::{MetadataPrintout, SpirvBuilder};
22

3-
fn main() {
3+
fn main() -> Result<(), Box<dyn std::error::Error>> {
44
let result = SpirvBuilder::new(
55
concat!(env!("CARGO_MANIFEST_DIR"), "/../shaders/sky-shader"),
66
"spirv-unknown-spv1.3",
7-
)
7+
)?
88
.print_metadata(MetadataPrintout::DependencyOnly)
99
.multimodule(true)
10-
.build()
11-
.unwrap();
10+
.build()?;
1211
println!("{result:#?}");
12+
Ok(())
1313
}

examples/runners/ash/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ use winit::{
7979
event_loop::{ControlFlow, EventLoop},
8080
};
8181

82+
use clap::Parser;
83+
use std::error::Error;
8284
use std::{
8385
borrow::Cow,
8486
collections::HashMap,
@@ -89,8 +91,6 @@ use std::{
8991
thread,
9092
};
9193

92-
use clap::Parser;
93-
9494
use spirv_builder::{MetadataPrintout, SpirvBuilder};
9595

9696
use shared::ShaderConstants;
@@ -235,7 +235,7 @@ pub fn main() {
235235
.unwrap();
236236
}
237237

238-
pub fn compile_shaders() -> Vec<SpvFile> {
238+
pub fn compile_shaders() -> Result<Vec<SpvFile>, Box<dyn Error>> {
239239
// Hack: spirv_builder builds into a custom directory if running under cargo, to not
240240
// deadlock, and the default target directory if not. However, packages like `proc-macro2`
241241
// have different configurations when being built here vs. when building
@@ -248,7 +248,7 @@ pub fn compile_shaders() -> Vec<SpvFile> {
248248
SpirvBuilder::new(
249249
concat!(env!("CARGO_MANIFEST_DIR"), "/../../shaders/sky-shader"),
250250
"spirv-unknown-vulkan1.1",
251-
)
251+
)?
252252
.print_metadata(MetadataPrintout::None)
253253
.shader_panic_strategy(spirv_builder::ShaderPanicStrategy::DebugPrintfThenExit {
254254
print_inputs: true,
@@ -257,8 +257,7 @@ pub fn compile_shaders() -> Vec<SpvFile> {
257257
// HACK(eddyb) needed because of `debugPrintf` instrumentation limitations
258258
// (see https://github.com/KhronosGroup/SPIRV-Tools/issues/4892).
259259
.multimodule(true)
260-
.build()
261-
.unwrap()
260+
.build()?
262261
.module
263262
.unwrap_multi()
264263
.iter()

examples/runners/wgpu/builder/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::Path;
77
fn build_shader(path_to_crate: &str, codegen_names: bool) -> Result<(), Box<dyn Error>> {
88
let builder_dir = &Path::new(env!("CARGO_MANIFEST_DIR"));
99
let path_to_crate = builder_dir.join(path_to_crate);
10-
let result = SpirvBuilder::new(path_to_crate, "spirv-unknown-vulkan1.1")
10+
let result = SpirvBuilder::new(path_to_crate, "spirv-unknown-vulkan1.1")?
1111
.print_metadata(MetadataPrintout::Full)
1212
.build()?;
1313
if codegen_names {

examples/runners/wgpu/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ fn maybe_watch(
154154

155155
let builder = SpirvBuilder::new(crate_path, "spirv-unknown-vulkan1.1")
156156
.print_metadata(MetadataPrintout::None)
157+
.unwrap()
157158
.shader_panic_strategy(if has_debug_printf {
158159
spirv_builder::ShaderPanicStrategy::DebugPrintfThenExit {
159160
print_inputs: true,

tests/difftests/lib/src/scaffold/compute/wgpu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl ComputeShader for RustComputeShader {
3636
&self,
3737
device: &wgpu::Device,
3838
) -> anyhow::Result<(wgpu::ShaderModule, Option<String>)> {
39-
let builder = SpirvBuilder::new(&self.path, "spirv-unknown-vulkan1.1")
39+
let builder = SpirvBuilder::new(&self.path, "spirv-unknown-vulkan1.1")?
4040
.print_metadata(spirv_builder::MetadataPrintout::None)
4141
.release(true)
4242
.multimodule(false)

0 commit comments

Comments
 (0)