Skip to content

Commit

Permalink
Add get_pipeline_cache_data + extend create_pipeline_cache with data
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakor Gyula committed Dec 6, 2018
1 parent 1cf6011 commit 881aab3
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/backend/dx11/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,14 @@ impl hal::Device<Backend> for Device {
})
}

fn create_pipeline_cache(&self) -> Result<(), device::OutOfMemory> {
fn create_pipeline_cache(&self, _data: Option<Vec<u8>>, device: &PhysicalDevice) -> Result<(), device::OutOfMemory> {
Ok(())
}

fn get_pipeline_cache_data(&self, cache: &()) -> Result<Vec<u8>, device::OutOfMemory> {
Ok(Vec::new())
}

fn destroy_pipeline_cache(&self, _: ()) {
//empty
}
Expand Down
6 changes: 5 additions & 1 deletion src/backend/dx12/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,10 +1433,14 @@ impl d::Device<B> for Device {
})
}

fn create_pipeline_cache(&self) -> Result<(), d::OutOfMemory> {
fn create_pipeline_cache(&self, _data: Option<Vec<u8>>, device: &PhysicalDevice) -> Result<(), d::OutOfMemory> {
Ok(())
}

fn get_pipeline_cache_data(&self, cache: &()) -> Result<Vec<u8>, d::OutOfMemory> {
Ok(Vec::new())
}

fn destroy_pipeline_cache(&self, _: ()) {
//empty
}
Expand Down
6 changes: 5 additions & 1 deletion src/backend/empty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ impl hal::Device<Backend> for Device {
unimplemented!()
}

fn create_pipeline_cache(&self) -> Result<(), device::OutOfMemory> {
fn create_pipeline_cache(&self, _data: Option<Vec<u8>>, device: &PhysicalDevice) -> Result<(), device::OutOfMemory> {
unimplemented!()
}

fn get_pipeline_cache_data(&self, cache: &()) -> Result<Vec<u8>, device::OutOfMemory> {
unimplemented!()
}

Expand Down
6 changes: 5 additions & 1 deletion src/backend/gl/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,14 @@ impl d::Device<B> for Device {
})
}

fn create_pipeline_cache(&self) -> Result<(), d::OutOfMemory> {
fn create_pipeline_cache(&self, _data: Option<Vec<u8>>, device: &PhysicalDevice) -> Result<(), d::OutOfMemory> {
Ok(())
}

fn get_pipeline_cache_data(&self, cache: &()) -> Result<Vec<u8>, d::OutOfMemory> {
Ok(Vec::new())
}

fn destroy_pipeline_cache(&self, _: ()) {
//empty
}
Expand Down
6 changes: 5 additions & 1 deletion src/backend/metal/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,12 +1157,16 @@ impl hal::Device<Backend> for Device {
})
}

fn create_pipeline_cache(&self) -> Result<n::PipelineCache, OutOfMemory> {
fn create_pipeline_cache(&self, _data: Option<Vec<u8>>) -> Result<n::PipelineCache, OutOfMemory> {
Ok(n::PipelineCache {
modules: FastStorageMap::default(),
})
}

fn get_pipeline_cache_data(&self, cache: &n::PipelineCache) -> Result<Vec<u8>, OutOfMemory> {
Ok(Vec::new())
}

fn destroy_pipeline_cache(&self, _cache: n::PipelineCache) {
//drop
}
Expand Down
69 changes: 66 additions & 3 deletions src/backend/vulkan/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::Arc;
use {Backend as B, Device};
use {conv, native as n, result, window as w};
use pool::RawCommandPool;
use PhysicalDevice;


#[derive(Debug)]
Expand Down Expand Up @@ -285,13 +286,61 @@ impl d::Device<B> for Device {
}
}

fn create_pipeline_cache(&self) -> Result<n::PipelineCache, d::OutOfMemory> {
fn create_pipeline_cache(&self, data: Option<Vec<u8>>, device: &PhysicalDevice) -> Result<n::PipelineCache, d::OutOfMemory> {
let (data_len, data) = match data {
Some(d) => {
let mut bad_cache = false;
let header = unsafe {
std::mem::transmute::<[u8; 4], u32>([d[0], d[1], d[2], d[3]]) };
if header <= 0 {
warn!("Bad header length");
bad_cache = true;
}

let cache_header_version = unsafe {
std::mem::transmute::<[u8; 4], u32>([d[4], d[5], d[6], d[7]]) };

if cache_header_version != 1 {
warn!("Unsupported cache header version");
bad_cache = true;
}

let vendor_id = unsafe {
std::mem::transmute::<[u8; 4], u32>([d[8], d[9], d[10], d[11]]) };

if vendor_id != device.properties.vendor_id as u32 {
warn!("Vendor ID mismatch");
bad_cache = true;
}

let device_id = unsafe {
std::mem::transmute::<[u8; 4], u32>([d[12], d[13], d[14], d[15]]) };

if device_id != device.properties.device_id as u32 {
warn!("Device ID mismatch");
bad_cache = true;
}

if device.properties.pipeline_cache_uuid != d[16 .. 32] {
warn!("Pipeline cache UUID mismatch device :{:?}, cache {:?}",
device.properties.pipeline_cache_uuid,
d[16 .. 32].to_vec());
bad_cache = true;
}
if bad_cache {
(0_usize, ptr::null())
} else {
(d.len(), d.as_ptr())
}
},
None => (0_usize, ptr::null()),
};
let info = vk::PipelineCacheCreateInfo {
s_type: vk::StructureType::PipelineCacheCreateInfo,
p_next: ptr::null(),
flags: vk::PipelineCacheCreateFlags::empty(),
initial_data_size: 0, //TODO
p_initial_data: ptr::null(),
initial_data_size: data_len,
p_initial_data: data as _,
};

let result = unsafe {
Expand All @@ -307,6 +356,20 @@ impl d::Device<B> for Device {
}
}

fn get_pipeline_cache_data(&self, cache: &n::PipelineCache) -> Result<Vec<u8>, d::OutOfMemory> {
let result = unsafe {
self.raw.0
.get_pipeline_cache_data(cache.raw)
};

match result {
Ok(data) => Ok(data),
Err(vk::Result::ErrorOutOfHostMemory) => Err(d::OutOfMemory::OutOfHostMemory),
Err(vk::Result::ErrorOutOfDeviceMemory) => Err(d::OutOfMemory::OutOfDeviceMemory),
_ => unreachable!(),
}
}

fn destroy_pipeline_cache(&self, cache: n::PipelineCache) {
unsafe {
self.raw.0.destroy_pipeline_cache(cache.raw, None)
Expand Down
6 changes: 4 additions & 2 deletions src/hal/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,10 @@ pub trait Device<B: Backend>: Any + Send + Sync {
fn destroy_pipeline_layout(&self, layout: B::PipelineLayout);

/// Create a pipeline cache object.
//TODO: allow loading from disk
fn create_pipeline_cache(&self) -> Result<B::PipelineCache, OutOfMemory>;
fn create_pipeline_cache(&self, data: Option<Vec<u8>>, device: &B::PhysicalDevice) -> Result<B::PipelineCache, OutOfMemory>;

/// Retrieve data from pipeline cache object.
fn get_pipeline_cache_data(&self, cache: &B::PipelineCache) -> Result<Vec<u8>, OutOfMemory>;

/// Merge a number of source pipeline caches into the target one.
fn merge_pipeline_caches<I>(&self, target: &B::PipelineCache, sources: I) -> Result<(), OutOfMemory>
Expand Down

0 comments on commit 881aab3

Please sign in to comment.