diff --git a/crates/metal2vulkan-runtime/src/pipeline/graphics.rs b/crates/metal2vulkan-runtime/src/pipeline/graphics.rs index 2e9b2f877..8fedacd7f 100644 --- a/crates/metal2vulkan-runtime/src/pipeline/graphics.rs +++ b/crates/metal2vulkan-runtime/src/pipeline/graphics.rs @@ -1347,24 +1347,26 @@ impl VariantObjects { let mut flag = vk::GraphicsPipelineLibraryCreateInfoEXT::default().flags(part.flag()); let info = infos.part(part, &mut flag); - let create = |cache| { - // SAFETY: every pointer in `info` outlives the call; the pipeline cache is - // the making's own, destroyed once the call returned, or none. - unsafe { shared.vk().create_graphics_pipelines(cache, &[info], None) } - }; // A part with shaders is made through the device's shader cache, under a record // of its own; one without holds nothing a record would keep. let made = match part.holds_shaders() { true => { let _ = creation_log::take_record(); let made = - shared.make_pipelines(Making::new(part.kind()).digest(name), create); + self.library_of_shaders(Making::new(part.kind()).digest(name), info); made_here = Some(creation_log::take_record()); made } false => { made_here = Some(None); - create(vk::PipelineCache::null()) + // SAFETY: every pointer in `info` outlives the call. + unsafe { + shared.vk().create_graphics_pipelines( + vk::PipelineCache::null(), + &[info], + None, + ) + } } }; let made = made.map_err(|(_, error)| vulkan(error))?; @@ -1405,6 +1407,50 @@ impl VariantObjects { .copied() .ok_or_else(|| internal("the driver linked no pipeline")) } + + /// The library of `info`, a part that holds shaders, made through the device's shader cache + /// under the record `making` names, with what its driver reports of it as a whole pipeline's + /// making has (`shader_cache::found_in_cache`, counted in `driver_pipeline_hits` and + /// `misses`): one found in a cache loaded from its record reads no data back and writes + /// nothing (`shader_cache/driver.rs`), whatever bytes the driver's data holds after it. + fn library_of_shaders( + &self, + making: Making, + info: vk::GraphicsPipelineCreateInfo<'_>, + ) -> Result, (Vec, vk::Result)> { + let shared = self.device.shared(); + let driver_reports = shared.shader_cache.driver_reports(); + // Its stages' feedback where it has at most `REPORTED_STAGES`, none where more (a count + // of 0 is valid, one equal to `stageCount` else: + // VUID-VkGraphicsPipelineCreateInfo-pipelineStageCreationFeedbackCount-06594). + let mut whole = vk::PipelineCreationFeedback::default(); + let mut stages = [vk::PipelineCreationFeedback::default(); REPORTED_STAGES]; + let count = info.stage_count as usize; + let reported_stages = if count <= REPORTED_STAGES { count } else { 0 }; + let made = shared.make_pipelines_reported(making, |cache| { + let reported = &mut stages[..reported_stages]; + let mut feedback = vk::PipelineCreationFeedbackCreateInfo::default() + .pipeline_creation_feedback(&mut whole) + .pipeline_stage_creation_feedbacks(reported); + let mut info = info; + if driver_reports { + info = info.push_next(&mut feedback); + } + // SAFETY: every pointer in `info` outlives the call; the pipeline cache is the + // device's for the record, or none. + let made = unsafe { shared.vk().create_graphics_pipelines(cache, &[info], None) }; + let found = driver_reports + .then(|| found_in_cache(&whole, &stages[..reported_stages])) + .flatten(); + (made, found) + }); + if driver_reports && made.is_ok() { + shared + .shader_cache + .driver_reported(&whole, &stages[..reported_stages]); + } + made + } } #[cfg(test)] diff --git a/crates/metal2vulkan-runtime/src/shader_cache/driver.rs b/crates/metal2vulkan-runtime/src/shader_cache/driver.rs index dcd09dbd6..8a379dee3 100644 --- a/crates/metal2vulkan-runtime/src/shader_cache/driver.rs +++ b/crates/metal2vulkan-runtime/src/shader_cache/driver.rs @@ -30,9 +30,14 @@ //! library map iterates its modules' keys opposite to their insertion), so the byte comparison //! alone wrote every record of two modules again at each warm making. The data's format is //! MoltenVK's own, undocumented beyond Vulkan's header, so no comparison here reads its -//! entries. A making whose driver reports nothing, or reports a compile, or whose cache was -//! made empty, reads the data and compares its SHA-256 with what the cache was loaded with or -//! last wrote. +//! entries. A library of shaders, where the device links its render pipelines from graphics +//! pipeline libraries, is made with its driver's report as a whole pipeline is: RADV's data +//! after a library's making from its record is now and then not the record's byte for byte, +//! one byte of over a thousand differing at the same length (Mesa 26.2.6 on a Radeon 780M: +//! both libraries of a state, in 2 runs of 20), which the byte comparison alone took for data +//! the making added, and wrote the record again. A making whose driver reports nothing, or +//! reports a compile, or whose cache was made empty, reads the data and compares its SHA-256 +//! with what the cache was loaded with or last wrote. //! - **Named by what a stage's conversion reads** ([`Making`]). A record is named by the SHA-256 //! of [`VERSION`], the making's description, the driver's identity (vendor, device, driver //! version, `pipelineCacheUUID`) and the digest of the layer settings the instance was created diff --git a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs index 0c6d09b9f..ea3bfd1ff 100644 --- a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs +++ b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs @@ -250,7 +250,9 @@ pub struct ShaderCacheStatistics { /// `APPLICATION_PIPELINE_CACHE_HIT`, or every stage's feedback is valid and says it. Drivers /// differ in where they say it: most for the whole pipeline; MoltenVK 1.4.1 for each stage /// alone, whose Metal function it found converted in the cache (its whole pipeline's - /// feedback is only ever valid). + /// feedback is only ever valid). Where the device links render pipelines from graphics + /// pipeline libraries, each library of shaders is such a making (its pre-rasterization or + /// fragment shader part), and the link itself is none. pub driver_pipeline_hits: u64, /// Such pipelines whose valid feedback says no hit, for the whole pipeline or for every /// stage: compiled, or a stage converted again. diff --git a/crates/metal2vulkan-runtime/src/tests/driver_pipeline_compile.rs b/crates/metal2vulkan-runtime/src/tests/driver_pipeline_compile.rs index 8f77b04d9..c210a0861 100644 --- a/crates/metal2vulkan-runtime/src/tests/driver_pipeline_compile.rs +++ b/crates/metal2vulkan-runtime/src/tests/driver_pipeline_compile.rs @@ -52,6 +52,11 @@ //! library of shaders, so nothing of its stages is converted or compiled again; and no making //! asks for a cache, so none is shared. //! +//! **A library of shaders reports what its driver found**, as a whole pipeline's making does: a +//! later device over the directory an earlier one wrote counts one report per library of shaders +//! it makes, and reads no data back for one found in its record +//! (`a_library_of_shaders_reports_what_its_driver_found`). +//! //! The device takes a shader cache directory of its own, empty, so no run reads a record an //! earlier run stored. //! @@ -167,3 +172,78 @@ fn every_sibling_render_creation_takes_the_first_s_driver_cache() { {RUN} found there, their stages being the same: {hits} hits and {misses} misses" ); } + +/// Where the device links its render pipelines from libraries and its makings report, the making +/// of each library of shaders reports what its driver found, as a whole pipeline's making does: a +/// later device over the directory an earlier one's creation of the same state wrote counts one +/// report for each of the state's pre-rasterization and fragment shader libraries, and each one +/// the driver reports found in the cache its record loaded reads no data back and writes nothing +/// (`driver_data_found`). Unreported, a library's making read its data back and compared the +/// bytes, and RADV's data after a library found in its record is now and then not the record's +/// byte for byte (`shader_cache/driver.rs`), so such a device wrote the record again. +#[test] +fn a_library_of_shaders_reports_what_its_driver_found() { + let scratch = Scratch::new("driver-library-reports"); + let options = PipelineOptions::default(); + { + let device = device(&scratch.0); + let shared = device.shared(); + if shared.libraries.is_none() || !shared.shader_cache.driver_reports() { + println!( + "DRIVER-LIBRARY-REPORTS: not covered (links from libraries: {}, makings report: \ + {})", + shared.libraries.is_some(), + shared.shader_cache.driver_reports() + ); + return; + } + drop( + device + .new_render_pipeline_state(&descriptor(&device), &options) + .unwrap(), + ); + } + // The first device is gone, so its cache thread wrote what its makings handed it. + let device = device(&scratch.0); + let base = descriptor(&device); + let before = device.shader_cache_statistics(); + let counts_before = device.work_counts(); + let _state = device.new_render_pipeline_state(&base, &options).unwrap(); + let after = device.shader_cache_statistics(); + let counts = device.work_counts(); + let made = |of: fn(&crate::WorkCounts) -> u64| of(&counts) - of(&counts_before); + let since = |of: fn(&crate::ShaderCacheStatistics) -> u64| of(&after) - of(&before); + let (hits, misses) = ( + since(|s| s.driver_pipeline_hits), + since(|s| s.driver_pipeline_misses), + ); + let (found, saves) = (since(|s| s.driver_data_found), since(|s| s.driver_saves)); + println!( + "DRIVER-LIBRARY-REPORTS: {hits} hits and {misses} misses reported, {found} found and read \ + nothing back, {saves} records' data written, {} loaded", + since(|s| s.driver_loads) + ); + // The fresh device makes each of the state's four libraries once and links them. + assert_eq!( + ( + made(|c| c.pipelines_linked), + made(|c| c.pipeline_libraries_made), + made(|c| c.fragment_shader_libraries_made) + ), + (1, 4, 1), + "the state links one pipeline from four libraries made for it" + ); + assert_eq!( + hits + misses, + 2, + "each of the state's two libraries of shaders reports what its driver found: {hits} hits \ + and {misses} misses" + ); + assert_eq!( + found, hits, + "each library its driver reports found in the cache its record loaded reads no data back" + ); + if hits == 2 { + assert_eq!(saves, 0, "libraries found in their records write nothing"); + } +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/pipeline_records.rs b/crates/metal2vulkan-validation/src/runtime_lane/pipeline_records.rs index 1289ec8c3..c6eb32dc9 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/pipeline_records.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/pipeline_records.rs @@ -13,11 +13,12 @@ //! draw's target byte for byte the first device's. A making handed a cache loaded from its //! record that the driver reports found there reads no data back and writes nothing //! (`driver_data_found`: MoltenVK's data after such a making is the record's entries in the -//! other order, which a byte comparison took for a change at every making). One the driver does -//! not report found writes it again exactly where the driver's data after the making is no -//! longer what the cache was loaded with or last wrote (RADV's after its first hit from a -//! compile's record is not, and settles), and counts every other read unchanged -//! (`driver_data_unchanged`). +//! other order, which a byte comparison took for a change at every making, and RADV's now and +//! then differs from the record's in a byte at the same length). Where the device links its +//! pipelines from libraries, each library of shaders is such a making and reports as a whole +//! pipeline does. One the driver does not report found writes it again exactly where the +//! driver's data after the making is no longer what the cache was loaded with or last wrote, +//! and counts every other read unchanged (`driver_data_unchanged`). //! - **Lines from the triangles' cache.** A state's line pipeline, made at its first line draw, //! names the record its triangle sibling named, which the state's creation made the cache of: //! the topology reaches the driver's conversion of a stage through the point class alone