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

Use Binary Archives as a pipeline cache on the metal backend #3719

Merged
merged 24 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a35f8d0
First draft of a pipeline cache for metal using binary archives!
expenses Apr 6, 2021
4c2fb06
Load pipeline caches from disk!
expenses Apr 6, 2021
3cf9016
Clean up quad example a little bit
expenses Apr 6, 2021
c36ae38
Add warning to merge_pipeline_caches
expenses Apr 6, 2021
0eb9340
Handle empty data inputs to create_pipeline_cache
expenses Apr 6, 2021
c5bcd27
Tested that pipeline caching does indeed work!
expenses Apr 7, 2021
39b9bdc
Do the same for compute pipelines
expenses Apr 7, 2021
a1063dd
Make the binary archive optional based on features
expenses Apr 7, 2021
f729ce6
Don't use a Mutex for the BinaryArchive.
expenses Apr 7, 2021
fb47035
Add a spv->msl cache
expenses Apr 9, 2021
fbc4387
Switch to git patches
expenses Apr 9, 2021
f74a293
Serialize/Deserialize both caches
expenses Apr 9, 2021
9294a13
Merge remote-tracking branch 'origin/master' into metal-pipeline-cache
expenses Apr 13, 2021
6aabde7
use a spv->msl cache for naga instead.
expenses Apr 13, 2021
d0e7e40
Use a git patch for naga instead of a path
expenses Apr 13, 2021
6344e31
Apply some suggestions
expenses Apr 14, 2021
fd6ff4e
Make a lot of things dependent on a pipeline-cache feature
expenses Apr 14, 2021
034a78e
Update naga
expenses Apr 17, 2021
e0b76d9
Merge remote-tracking branch 'origin/master' into metal-pipeline-cache
expenses Apr 17, 2021
9e27f52
Use latest naga on the gl and vulkan backends
expenses Apr 18, 2021
cdf771c
Merge remote-tracking branch 'origin/master' into metal-pipeline-cache
expenses Apr 19, 2021
fab2b4a
Apply suggestions wrt u8s
expenses Apr 19, 2021
9dbf6b7
Move pipeline cache stuff to its own module
expenses Apr 19, 2021
ce89c9c
Switch to naga gfx-22
expenses Apr 19, 2021
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
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