Skip to content

Commit 4c9718f

Browse files
Firestar99schell
andauthored
Shader crate cargo features (#13)
* feat: allow shader crate cargo features to be passed through spirv-builder --------- Co-authored-by: Schell Carl Scivally <[email protected]> Co-authored-by: Firestar99 <[email protected]>
1 parent de7ba8e commit 4c9718f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333
- Signed for loops like `for _ in 0..4i32 {}` no longer compile. We recommend switching to unsigned for loops and casting back to signed integers in the meanwhile.
3434

3535
### Changed 🛠
36+
- [PR#13](https://github.com/Rust-GPU/rust-gpu/pull/13) allow cargo features to be passed to the shader crate
3637
- [PR#12](https://github.com/rust-gpu/rust-gpu/pull/12) updated toolchain to `nightly-2024-04-24`
3738
- [PR#9](https://github.com/Rust-GPU/rust-gpu/pull/9) relaxed `glam` version requirements (`>=0.22, <=0.29`)
3839
- [PR#1127](https://github.com/EmbarkStudios/rust-gpu/pull/1127) updated `spirv-tools` to `0.10.0`, which follows `vulkan-sdk-1.3.275`

crates/spirv-builder/src/lib.rs

+35
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,19 @@ pub enum ShaderPanicStrategy {
282282
UNSOUND_DO_NOT_USE_UndefinedBehaviorViaUnreachable,
283283
}
284284

285+
/// Cargo features specification for building the shader crate.
286+
#[derive(Default)]
287+
struct ShaderCrateFeatures {
288+
default_features: Option<bool>,
289+
features: Vec<String>,
290+
}
291+
285292
pub struct SpirvBuilder {
286293
path_to_crate: PathBuf,
287294
print_metadata: MetadataPrintout,
288295
release: bool,
289296
target: String,
297+
shader_crate_features: ShaderCrateFeatures,
290298
deny_warnings: bool,
291299
multimodule: bool,
292300
spirv_metadata: SpirvMetadata,
@@ -333,6 +341,7 @@ impl SpirvBuilder {
333341
skip_block_layout: false,
334342

335343
preserve_bindings: false,
344+
shader_crate_features: ShaderCrateFeatures::default(),
336345
}
337346
}
338347

@@ -459,6 +468,20 @@ impl SpirvBuilder {
459468
self
460469
}
461470

471+
/// Set --default-features for the target shader crate.
472+
#[must_use]
473+
pub fn shader_crate_default_features(mut self, default_features: bool) -> Self {
474+
self.shader_crate_features.default_features = Some(default_features);
475+
self
476+
}
477+
478+
/// Set --features for the target shader crate.
479+
#[must_use]
480+
pub fn shader_crate_features(mut self, features: impl IntoIterator<Item = String>) -> Self {
481+
self.shader_crate_features.features = features.into_iter().collect();
482+
self
483+
}
484+
462485
/// Builds the module. If `print_metadata` is [`MetadataPrintout::Full`], you usually don't have to inspect the path
463486
/// in the result, as the environment variable for the path to the module will already be set.
464487
pub fn build(mut self) -> Result<CompileResult, SpirvBuilderError> {
@@ -755,6 +778,18 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
755778
.join(format!("{}.json", builder.target)),
756779
);
757780

781+
if let Some(default_features) = builder.shader_crate_features.default_features {
782+
if !default_features {
783+
cargo.arg("--no-default-features");
784+
}
785+
}
786+
787+
if !builder.shader_crate_features.features.is_empty() {
788+
cargo
789+
.arg("--features")
790+
.arg(builder.shader_crate_features.features.join(","));
791+
}
792+
758793
// NOTE(eddyb) see above how this is computed and why it might be missing.
759794
if let Some(target_dir) = target_dir {
760795
cargo.arg("--target-dir").arg(target_dir);

0 commit comments

Comments
 (0)