Skip to content

Commit

Permalink
Use Binary Archives as a pipeline cache on the metal backend (#3719)
Browse files Browse the repository at this point in the history
* First draft of a pipeline cache for metal using binary archives!

* Load pipeline caches from disk!

* Clean up quad example a little bit

* Add warning to merge_pipeline_caches

* Handle empty data inputs to create_pipeline_cache

* Tested that pipeline caching does indeed work!

* Do the same for compute pipelines

* Make the binary archive optional based on features

* Don't use a Mutex for the BinaryArchive.

* Add a spv->msl cache

* Switch to git patches

* Serialize/Deserialize both caches

* use a spv->msl cache for naga instead.

* Use a git patch for naga instead of a path

* Apply some suggestions

* Make a lot of things dependent on a pipeline-cache feature

* Update naga

* Use latest naga on the gl and vulkan backends

* Apply suggestions wrt u8s

* Move pipeline cache stuff to its own module

* Switch to naga gfx-22
  • Loading branch information
expenses committed Apr 19, 2021
1 parent 46d7625 commit 6999171
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 165 deletions.
35 changes: 34 additions & 1 deletion examples/quad/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ struct Renderer<B: hal::Backend> {
framebuffer: ManuallyDrop<B::Framebuffer>,
pipeline: ManuallyDrop<B::GraphicsPipeline>,
pipeline_layout: ManuallyDrop<B::PipelineLayout>,
pipeline_cache: ManuallyDrop<B::PipelineCache>,
desc_set: Option<B::DescriptorSet>,
set_layout: ManuallyDrop<B::DescriptorSetLayout>,
submission_complete_semaphores: Vec<B::Semaphore>,
Expand Down Expand Up @@ -707,6 +708,25 @@ where
cmd_buffers.push(unsafe { cmd_pools[i].allocate_one(command::Level::Primary) });
}

let pipeline_cache_path = "quad_pipeline_cache";

let previous_pipeline_cache_data = std::fs::read(pipeline_cache_path);

if let Err(error) = previous_pipeline_cache_data.as_ref() {
println!("Error loading the previous pipeline cache data: {}", error);
}

let pipeline_cache = ManuallyDrop::new(unsafe {
device
.create_pipeline_cache(
previous_pipeline_cache_data
.as_ref()
.ok()
.map(|vec| &vec[..]),
)
.expect("Can't create pipeline cache")
});

let pipeline_layout = ManuallyDrop::new(
unsafe { device.create_pipeline_layout(iter::once(&*set_layout), iter::empty()) }
.expect("Can't create pipeline layout"),
Expand Down Expand Up @@ -793,7 +813,7 @@ where
blend: Some(pso::BlendState::ALPHA),
});

unsafe { device.create_graphics_pipeline(&pipeline_desc, None) }
unsafe { device.create_graphics_pipeline(&pipeline_desc, Some(&pipeline_cache)) }
};

unsafe {
Expand All @@ -806,6 +826,16 @@ where
ManuallyDrop::new(pipeline.unwrap())
};

let pipeline_cache_data =
unsafe { device.get_pipeline_cache_data(&pipeline_cache).unwrap() };

std::fs::write(pipeline_cache_path, &pipeline_cache_data).unwrap();
log::info!(
"Wrote the pipeline cache to {} ({} bytes)",
pipeline_cache_path,
pipeline_cache_data.len()
);

// Rendering setup
let viewport = pso::Viewport {
rect: pso::Rect {
Expand All @@ -831,6 +861,7 @@ where
framebuffer,
pipeline,
pipeline_layout,
pipeline_cache,
desc_set: Some(desc_set),
set_layout,
submission_complete_semaphores,
Expand Down Expand Up @@ -1029,6 +1060,8 @@ where
.destroy_pipeline_layout(ManuallyDrop::into_inner(ptr::read(
&self.pipeline_layout,
)));
self.device
.destroy_pipeline_cache(ManuallyDrop::into_inner(ptr::read(&self.pipeline_cache)));
let surface = ManuallyDrop::into_inner(ptr::read(&self.surface));
self.instance.destroy_surface(surface);
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/gl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ optional = true

[dependencies.naga]
git = "https://github.com/gfx-rs/naga"
tag = "gfx-21"
tag = "gfx-22"
features = ["spv-in", "glsl-out"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
8 changes: 6 additions & 2 deletions src/backend/metal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2018"
default = []
signpost = []
cross = ["spirv_cross", "auxil", "naga/spv-out"]
pipeline-cache = ["tempfile", "serde", "bincode", "naga/serialize", "naga/deserialize", "naga/spv-out"]

[lib]
name = "gfx_backend_metal"
Expand All @@ -29,7 +30,7 @@ copyless = "0.1.4"
fxhash = "0.2.1"
log = { version = "0.4" }
dispatch = { version = "0.2", optional = true }
metal = { git = "https://github.com/gfx-rs/metal-rs", rev="439c986eb7a9b91e88b61def2daa66e4043fcbef", features = ["private"] }
metal = { git = "https://github.com/gfx-rs/metal-rs", rev="78f632d194c7c16d18b71d7373c4080847d110b0", features = ["private"] }
foreign-types = "0.3"
objc = "0.2.5"
block = "0.1"
Expand All @@ -38,6 +39,9 @@ parking_lot = "0.11"
storage-map = "0.3"
raw-window-handle = "0.3"
profiling = { version = "0.1.10", default-features = false }
tempfile = { version = "3.2", optional = true }
serde = { version = "1", features = ["serde_derive"], optional = true }
bincode = { version = "1", optional = true }

[dependencies.auxil]
package = "gfx-auxil"
Expand All @@ -53,7 +57,7 @@ optional = true

[dependencies.naga]
git = "https://github.com/gfx-rs/naga"
tag = "gfx-21"
tag = "gfx-22"
features = ["spv-in", "msl-out"]

# This forces docs.rs to build the crate on mac, otherwise the build fails
Expand Down
Loading

0 comments on commit 6999171

Please sign in to comment.