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

More the build.rs scripts into the shaders #181

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/example-runner-cpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
[dependencies]
minifb = "0.19.1"
# bring in the shader as natively compiled code
example-shader = { path = "../example-shader" }
example-shader = { path = "../example-shader", default-features = false }
# for math types, likely get from glam directly later instead
spirv-std = { path = "../../spirv-std" }

Expand Down
8 changes: 3 additions & 5 deletions examples/example-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ license = "MIT OR Apache-2.0"
# See rustc_codegen_spirv/Cargo.toml for details on these features
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
use-installed-tools = ["example-shader/use-installed-tools"]
use-compiled-tools = ["example-shader/use-compiled-tools"]
Comment on lines -11 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really a fan of having to propagate these features, but I don't see a different way at the moment.


[dependencies]
ash = "0.31"
ash-window = "0.5"
cfg-if = "1.0.0"
structopt = "0.3.20"
winit = "0.23.0"
example-shader = { path = "../example-shader", default-features = false, features = ["build-spirv"] }

[target.'cfg(target_os = "macos")'.dependencies]
ash-molten = { git = "https://github.com/EmbarkStudios/ash-molten", branch = "moltenvk-1.1.0" }

[build-dependencies]
spirv-builder = { path = "../../spirv-builder", default-features = false }
2 changes: 1 addition & 1 deletion examples/example-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ fn main() {
base.device
.bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0)
.unwrap();
let mut spv_file = Cursor::new(&include_bytes!(env!("example_shader.spv"))[..]);
let mut spv_file = Cursor::new(example_shader::COMPILED_SHADER);
let code = read_spv(&mut spv_file).expect("Failed to read spv file");
let shader_info = vk::ShaderModuleCreateInfo::builder().code(&code);

Expand Down
9 changes: 9 additions & 0 deletions examples/example-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ crate-type = ["dylib"]

[dependencies]
spirv-std = { path = "../../spirv-std" }

[build-dependencies]
spirv-builder = { path = "../../spirv-builder", default-features = false, optional = true }

[features]
default = ["build-spirv", "use-compiled-tools"]
build-spirv = ["spirv-builder"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use spirv_builder::SpirvBuilder;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
// This will set the env var `example-shader.spv` to a spir-v file that can be include!()'d
SpirvBuilder::new("../example-shader").build()?;
#[cfg(feature = "build-spirv")]
spirv_builder::SpirvBuilder::new(".")
.spirv_version(1, 0)
.build()?;
Ok(())
}
3 changes: 3 additions & 0 deletions examples/example-shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use core::f32::consts::PI;
use spirv_std::{Input, Mat4, MathExt, Output, Vec2, Vec3, Vec4};

#[cfg(all(not(target_arch = "spirv"), feature = "build-spirv"))]
pub const COMPILED_SHADER: &'static [u8] = include_bytes!(env!("example_shader.spv"));

const DEPOLARIZATION_FACTOR: f32 = 0.035;
const LUMINANCE: f32 = 1.0;
const MIE_COEFFICIENT: f32 = 0.005;
Expand Down
8 changes: 3 additions & 5 deletions examples/wgpu-example-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ license = "MIT OR Apache-2.0"
# See rustc_codegen_spirv/Cargo.toml for details on these features
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
use-installed-tools = ["wgpu-example-shader/use-installed-tools"]
use-compiled-tools = ["wgpu-example-shader/use-compiled-tools"]

[dependencies]
wgpu = "0.6.0"
futures = { version = "0.3", default-features = false, features = ["std", "executor"] }
winit = { version = "0.22.1", features = ["web-sys"] }

[build-dependencies]
spirv-builder = { path = "../../spirv-builder", default-features = false }
wgpu-example-shader = { path = "../wgpu-example-shader", default-features = false, features = ["build-spirv"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
wgpu-subscriber = "0.1.0"
Expand Down
3 changes: 2 additions & 1 deletion examples/wgpu-example-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
.expect("Failed to create device");

// Load the shaders from disk
let module = device.create_shader_module(wgpu::include_spirv!(env!("wgpu_example_shader.spv")));
let shader = wgpu::util::make_spirv(wgpu_example_shader::COMPILED_SHADER);
let module = device.create_shader_module(shader);

let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
Expand Down
9 changes: 9 additions & 0 deletions examples/wgpu-example-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ crate-type = ["dylib"]

[dependencies]
spirv-std = { path = "../../spirv-std" }

[build-dependencies]
spirv-builder = { path = "../../spirv-builder", default-features = false, optional = true }

[features]
default = ["build-spirv", "use-compiled-tools"]
build-spirv = ["spirv-builder"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use spirv_builder::SpirvBuilder;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
// This will set the env var `wgpu-example-shader.spv` to a spir-v file that can be include!()'d
SpirvBuilder::new("../wgpu-example-shader")
#[cfg(feature = "build-spirv")]
spirv_builder::SpirvBuilder::new(".")
.spirv_version(1, 0)
.build()?;
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions examples/wgpu-example-shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use spirv_std::{Input, Output, Vec4};

#[cfg(all(not(target_arch = "spirv"), feature = "build-spirv"))]
pub const COMPILED_SHADER: &'static [u8] = include_bytes!(env!("wgpu_example_shader.spv"));

#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main_fs(mut output: Output<Vec4>) {
Expand Down
5 changes: 5 additions & 0 deletions spirv-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl SpirvBuilder {
/// you usually don't have to inspect the path, as the environment variable will already be
/// set.
pub fn build(self) -> Result<PathBuf, SpirvBuilderError> {
if std::env::var("TARGETING_SPIRV").is_ok() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to stop the build script from being called recursively.

return Ok(PathBuf::new());
}

let spirv_module = invoke_rustc(&self)?;
let env_var = spirv_module.file_name().unwrap().to_str().unwrap();
if self.print_metadata {
Expand Down Expand Up @@ -160,6 +164,7 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
.stderr(Stdio::inherit())
.current_dir(&builder.path_to_crate)
.env("RUSTFLAGS", rustflags)
.env("TARGETING_SPIRV", "1")
.output()
.expect("failed to execute cargo build");
if build.status.success() {
Expand Down