| author | |
| committer | |
| log | 1d0c6e86ad69ac238e43a212f8ee67bc4ba92d18 |
| tree | e7f49922b60c44462d210c2607679de78eb1a7a7 |
| parent | 200ab32029f147f893c3b65597794ffbf072ffd2 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
4 files changed, 500 insertions(+), 104 deletions(-)
crates/engine/src/custom_shader.rs+250-49| ... | ... | @@ -900,14 +900,20 @@ fn custom_tool_op(@builtin(global_invocation_id) gid: vec3<u32>) {{ |
| 900 | 900 | // Pipeline Cache & GPU Dispatch |
| 901 | 901 | // ============================================================================ |
| 902 | 902 | |
| 903 | const CUSTOM_UNIFORM_SLOTS: u32 = 256; | |
| 903 | const CUSTOM_UNIFORM_SLOTS_PER_CHUNK: u32 = 256; | |
| 904 | 904 | const CUSTOM_BIND_GROUP_CACHE_CAPACITY: usize = 128; |
| 905 | const MAX_CUSTOM_PIPELINES: usize = 128; | |
| 905 | 906 | |
| 906 | 907 | struct FormatLayouts { |
| 907 | 908 | bgl: wgpu::BindGroupLayout, |
| 908 | 909 | pl: wgpu::PipelineLayout, |
| 909 | 910 | } |
| 910 | 911 | |
| 912 | struct CustomPipelineEntry { | |
| 913 | pipeline: Option<wgpu::ComputePipeline>, | |
| 914 | last_used: u64, | |
| 915 | } | |
| 916 | ||
| 911 | 917 | struct CustomBindGroupKey { |
| 912 | 918 | format: wgpu::TextureFormat, |
| 913 | 919 | src_1: wgpu::TextureView, |
| ... | ... | @@ -915,9 +921,11 @@ struct CustomBindGroupKey { |
| 915 | 921 | src_3: wgpu::TextureView, |
| 916 | 922 | mask: wgpu::TextureView, |
| 917 | 923 | dst_view: wgpu::TextureView, |
| 924 | chunk_idx: usize, | |
| 918 | 925 | } |
| 919 | 926 | |
| 920 | 927 | impl CustomBindGroupKey { |
| 928 | #[allow(clippy::too_many_arguments)] | |
| 921 | 929 | fn matches( |
| 922 | 930 | &self, |
| 923 | 931 | format: wgpu::TextureFormat, |
| ... | ... | @@ -926,6 +934,7 @@ impl CustomBindGroupKey { |
| 926 | 934 | src_3: &wgpu::TextureView, |
| 927 | 935 | mask: &wgpu::TextureView, |
| 928 | 936 | dst_view: &wgpu::TextureView, |
| 937 | chunk_idx: usize, | |
| 929 | 938 | ) -> bool { |
| 930 | 939 | self.format == format |
| 931 | 940 | && &self.src_1 == src_1 |
| ... | ... | @@ -933,6 +942,7 @@ impl CustomBindGroupKey { |
| 933 | 942 | && &self.src_3 == src_3 |
| 934 | 943 | && &self.mask == mask |
| 935 | 944 | && &self.dst_view == dst_view |
| 945 | && self.chunk_idx == chunk_idx | |
| 936 | 946 | } |
| 937 | 947 | } |
| 938 | 948 | |
| ... | ... | @@ -949,6 +959,7 @@ fn hash_custom_bind_group_key( |
| 949 | 959 | src_3: &wgpu::TextureView, |
| 950 | 960 | mask: &wgpu::TextureView, |
| 951 | 961 | dst_view: &wgpu::TextureView, |
| 962 | chunk_idx: usize, | |
| 952 | 963 | ) -> u64 { |
| 953 | 964 | let mut h = DefaultHasher::new(); |
| 954 | 965 | format.hash(&mut h); |
| ... | ... | @@ -957,19 +968,21 @@ fn hash_custom_bind_group_key( |
| 957 | 968 | src_3.hash(&mut h); |
| 958 | 969 | mask.hash(&mut h); |
| 959 | 970 | dst_view.hash(&mut h); |
| 971 | chunk_idx.hash(&mut h); | |
| 960 | 972 | h.finish() |
| 961 | 973 | } |
| 962 | 974 | |
| 963 | 975 | /// Caches dynamically compiled `CustomTool` compute pipelines keyed by |
| 964 | /// `(dst_texture_format, hash_of_normalized_program)`, plus a persistent dynamic-offset | |
| 965 | /// uniform ring buffer and LRU-cached `BindGroup`s so repeated dispatches avoid per-frame | |
| 966 | /// GPU buffer/bind-group allocations. | |
| 976 | /// `(dst_texture_format, normalized_program)` with an LRU cap, plus a lazily growing | |
| 977 | /// chunked dynamic-offset uniform ring buffer and LRU-cached `BindGroup`s so repeated | |
| 978 | /// dispatches avoid per-frame GPU buffer/bind-group allocations. | |
| 967 | 979 | #[derive(Default)] |
| 968 | 980 | pub struct CustomShaderCache { |
| 969 | 981 | layouts: HashMap<wgpu::TextureFormat, FormatLayouts>, |
| 970 | 982 | sampler: Option<wgpu::Sampler>, |
| 971 | pipelines: HashMap<(wgpu::TextureFormat, u64), Option<wgpu::ComputePipeline>>, | |
| 972 | uniform_buf: Option<wgpu::Buffer>, | |
| 983 | pipelines: HashMap<(wgpu::TextureFormat, CustomProgram), CustomPipelineEntry>, | |
| 984 | pipeline_tick: u64, | |
| 985 | uniform_chunks: Vec<wgpu::Buffer>, | |
| 973 | 986 | uniform_stride: u32, |
| 974 | 987 | uniform_cursor: u32, |
| 975 | 988 | bg_cache: HashMap<u64, Vec<CustomBindGroupEntry>>, |
| ... | ... | @@ -1009,6 +1022,16 @@ impl CustomShaderCache { |
| 1009 | 1022 | self.uniform_cursor = 0; |
| 1010 | 1023 | } |
| 1011 | 1024 | |
| 1025 | /// Trims the dynamic uniform ring to at most `keep` chunks and resets the cursor. | |
| 1026 | /// Clears `bg_cache` whenever chunks are dropped so cached bind groups never reference freed buffers. | |
| 1027 | pub fn trim_uniform_chunks(&mut self, keep: usize) { | |
| 1028 | self.uniform_cursor = 0; | |
| 1029 | if self.uniform_chunks.len() > keep { | |
| 1030 | self.uniform_chunks.truncate(keep); | |
| 1031 | self.clear_bind_group_cache(); | |
| 1032 | } | |
| 1033 | } | |
| 1034 | ||
| 1012 | 1035 | fn ensure_format_layouts(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat) { |
| 1013 | 1036 | self.layouts.entry(format).or_insert_with(|| { |
| 1014 | 1037 | let tex_entry = |binding| wgpu::BindGroupLayoutEntry { |
| ... | ... | @@ -1076,21 +1099,13 @@ impl CustomShaderCache { |
| 1076 | 1099 | } |
| 1077 | 1100 | } |
| 1078 | 1101 | |
| 1079 | fn ensure_uniform_buffer(&mut self, device: &wgpu::Device) { | |
| 1080 | if self.uniform_buf.is_some() { | |
| 1102 | fn ensure_uniform_stride(&mut self, device: &wgpu::Device) { | |
| 1103 | if self.uniform_stride != 0 { | |
| 1081 | 1104 | return; |
| 1082 | 1105 | } |
| 1083 | 1106 | let uniform_size = std::mem::size_of::<CustomParams>() as u32; |
| 1084 | 1107 | let min_align = device.limits().min_uniform_buffer_offset_alignment.max(1); |
| 1085 | let stride = uniform_size.div_ceil(min_align) * min_align; | |
| 1086 | let buf = device.create_buffer(&wgpu::BufferDescriptor { | |
| 1087 | label: Some("custom-tool-params-ring"), | |
| 1088 | size: u64::from(CUSTOM_UNIFORM_SLOTS) * u64::from(stride), | |
| 1089 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, | |
| 1090 | mapped_at_creation: false, | |
| 1091 | }); | |
| 1092 | self.uniform_stride = stride; | |
| 1093 | self.uniform_buf = Some(buf); | |
| 1108 | self.uniform_stride = uniform_size.div_ceil(min_align) * min_align; | |
| 1094 | 1109 | } |
| 1095 | 1110 | |
| 1096 | 1111 | fn alloc_uniform_slot( |
| ... | ... | @@ -1098,14 +1113,29 @@ impl CustomShaderCache { |
| 1098 | 1113 | device: &wgpu::Device, |
| 1099 | 1114 | queue: &wgpu::Queue, |
| 1100 | 1115 | params: &CustomParams, |
| 1101 | ) -> u32 { | |
| 1102 | self.ensure_uniform_buffer(device); | |
| 1103 | let slot = self.uniform_cursor; | |
| 1104 | let offset = slot * self.uniform_stride; | |
| 1105 | let ubuf = self.uniform_buf.as_ref().expect("uniform buffer initialized"); | |
| 1106 | queue.write_buffer(ubuf, u64::from(offset), bytemuck::bytes_of(params)); | |
| 1107 | self.uniform_cursor = (slot + 1) % CUSTOM_UNIFORM_SLOTS; | |
| 1108 | offset | |
| 1116 | ) -> (usize, u32) { | |
| 1117 | self.ensure_uniform_stride(device); | |
| 1118 | let slot_global = self.uniform_cursor; | |
| 1119 | let chunk_idx = (slot_global / CUSTOM_UNIFORM_SLOTS_PER_CHUNK) as usize; | |
| 1120 | let slot_in_chunk = slot_global % CUSTOM_UNIFORM_SLOTS_PER_CHUNK; | |
| 1121 | while chunk_idx >= self.uniform_chunks.len() { | |
| 1122 | let size = u64::from(CUSTOM_UNIFORM_SLOTS_PER_CHUNK) * u64::from(self.uniform_stride); | |
| 1123 | let buf = device.create_buffer(&wgpu::BufferDescriptor { | |
| 1124 | label: Some("custom-tool-params-ring"), | |
| 1125 | size, | |
| 1126 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, | |
| 1127 | mapped_at_creation: false, | |
| 1128 | }); | |
| 1129 | self.uniform_chunks.push(buf); | |
| 1130 | } | |
| 1131 | let offset = slot_in_chunk * self.uniform_stride; | |
| 1132 | queue.write_buffer( | |
| 1133 | &self.uniform_chunks[chunk_idx], | |
| 1134 | u64::from(offset), | |
| 1135 | bytemuck::bytes_of(params), | |
| 1136 | ); | |
| 1137 | self.uniform_cursor = slot_global + 1; | |
| 1138 | (chunk_idx, offset) | |
| 1109 | 1139 | } |
| 1110 | 1140 | |
| 1111 | 1141 | #[allow(clippy::too_many_arguments)] |
| ... | ... | @@ -1118,8 +1148,9 @@ impl CustomShaderCache { |
| 1118 | 1148 | src_3: &wgpu::TextureView, |
| 1119 | 1149 | mask: &wgpu::TextureView, |
| 1120 | 1150 | dst_view: &wgpu::TextureView, |
| 1151 | chunk_idx: usize, | |
| 1121 | 1152 | ) -> (u64, usize) { |
| 1122 | let hash = hash_custom_bind_group_key(format, src_1, src_2, src_3, mask, dst_view); | |
| 1153 | let hash = hash_custom_bind_group_key(format, src_1, src_2, src_3, mask, dst_view, chunk_idx); | |
| 1123 | 1154 | self.bg_tick = self.bg_tick.wrapping_add(1); |
| 1124 | 1155 | let tick = self.bg_tick; |
| 1125 | 1156 | |
| ... | ... | @@ -1127,7 +1158,7 @@ impl CustomShaderCache { |
| 1127 | 1158 | && let Some((idx, entry)) = bucket |
| 1128 | 1159 | .iter_mut() |
| 1129 | 1160 | .enumerate() |
| 1130 | .find(|(_, e)| e.key.matches(format, src_1, src_2, src_3, mask, dst_view)) | |
| 1161 | .find(|(_, e)| e.key.matches(format, src_1, src_2, src_3, mask, dst_view, chunk_idx)) | |
| 1131 | 1162 | { |
| 1132 | 1163 | entry.last_used = tick; |
| 1133 | 1164 | self.bg_hits += 1; |
| ... | ... | @@ -1164,7 +1195,7 @@ impl CustomShaderCache { |
| 1164 | 1195 | |
| 1165 | 1196 | let layout = &self.layouts[&format].bgl; |
| 1166 | 1197 | let sampler = self.sampler.as_ref().expect("sampler initialized"); |
| 1167 | let ubuf = self.uniform_buf.as_ref().expect("uniform buffer initialized"); | |
| 1198 | let ubuf = &self.uniform_chunks[chunk_idx]; | |
| 1168 | 1199 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 1169 | 1200 | label: Some("custom-tool-bg"), |
| 1170 | 1201 | layout, |
| ... | ... | @@ -1196,6 +1227,7 @@ impl CustomShaderCache { |
| 1196 | 1227 | src_3: src_3.clone(), |
| 1197 | 1228 | mask: mask.clone(), |
| 1198 | 1229 | dst_view: dst_view.clone(), |
| 1230 | chunk_idx, | |
| 1199 | 1231 | }, |
| 1200 | 1232 | bg, |
| 1201 | 1233 | last_used: tick, |
| ... | ... | @@ -1233,28 +1265,53 @@ impl CustomShaderCache { |
| 1233 | 1265 | Some(pipeline) |
| 1234 | 1266 | } |
| 1235 | 1267 | |
| 1236 | /// Compiles and caches the compute pipeline for `program` at `format`, returning `false` | |
| 1237 | /// if the program contains unsupported syntax (graceful fallback to passthrough). | |
| 1238 | pub fn ensure_compiled( | |
| 1268 | fn ensure_compiled_key( | |
| 1239 | 1269 | &mut self, |
| 1240 | 1270 | device: &wgpu::Device, |
| 1241 | program: &CustomProgram, | |
| 1242 | format: wgpu::TextureFormat, | |
| 1271 | key: &(wgpu::TextureFormat, CustomProgram), | |
| 1243 | 1272 | ) -> bool { |
| 1244 | let norm = program.normalized(); | |
| 1245 | let key = (format, norm.program_hash()); | |
| 1246 | if let Some(entry) = self.pipelines.get(&key) { | |
| 1247 | return entry.is_some(); | |
| 1273 | self.pipeline_tick = self.pipeline_tick.wrapping_add(1); | |
| 1274 | let tick = self.pipeline_tick; | |
| 1275 | if let Some(entry) = self.pipelines.get_mut(key) { | |
| 1276 | entry.last_used = tick; | |
| 1277 | return entry.pipeline.is_some(); | |
| 1248 | 1278 | } |
| 1249 | let pipeline = match compile_wgsl(&norm, format) { | |
| 1250 | Ok(wgsl) => self.compile_wgsl_pipeline(device, format, &wgsl), | |
| 1279 | let pipeline = match compile_wgsl(&key.1, key.0) { | |
| 1280 | Ok(wgsl) => self.compile_wgsl_pipeline(device, key.0, &wgsl), | |
| 1251 | 1281 | Err(_) => None, |
| 1252 | 1282 | }; |
| 1253 | 1283 | let ok = pipeline.is_some(); |
| 1254 | self.pipelines.insert(key, pipeline); | |
| 1284 | if self.pipelines.len() >= MAX_CUSTOM_PIPELINES | |
| 1285 | && let Some(oldest_key) = self | |
| 1286 | .pipelines | |
| 1287 | .iter() | |
| 1288 | .min_by_key(|(_, entry)| entry.last_used) | |
| 1289 | .map(|(k, _)| k.clone()) | |
| 1290 | { | |
| 1291 | self.pipelines.remove(&oldest_key); | |
| 1292 | } | |
| 1293 | self.pipelines.insert( | |
| 1294 | key.clone(), | |
| 1295 | CustomPipelineEntry { | |
| 1296 | pipeline, | |
| 1297 | last_used: tick, | |
| 1298 | }, | |
| 1299 | ); | |
| 1255 | 1300 | ok |
| 1256 | 1301 | } |
| 1257 | 1302 | |
| 1303 | /// Compiles and caches the compute pipeline for `program` at `format`, returning `false` | |
| 1304 | /// if the program contains unsupported syntax (graceful fallback to passthrough). | |
| 1305 | pub fn ensure_compiled( | |
| 1306 | &mut self, | |
| 1307 | device: &wgpu::Device, | |
| 1308 | program: &CustomProgram, | |
| 1309 | format: wgpu::TextureFormat, | |
| 1310 | ) -> bool { | |
| 1311 | let key = (format, program.normalized()); | |
| 1312 | self.ensure_compiled_key(device, &key) | |
| 1313 | } | |
| 1314 | ||
| 1258 | 1315 | /// Dispatches a compiled `CustomProgram` into `dst_view`. Returns `false` if compilation failed. |
| 1259 | 1316 | #[allow(clippy::too_many_arguments)] |
| 1260 | 1317 | pub fn dispatch( |
| ... | ... | @@ -1272,18 +1329,25 @@ impl CustomShaderCache { |
| 1272 | 1329 | dst_size: [u32; 2], |
| 1273 | 1330 | params: &CustomParams, |
| 1274 | 1331 | ) -> bool { |
| 1275 | let norm = program.normalized(); | |
| 1276 | let key = (format, norm.program_hash()); | |
| 1277 | if !self.ensure_compiled(device, &norm, format) { | |
| 1278 | return false; | |
| 1279 | } | |
| 1280 | if !matches!(self.pipelines.get(&key), Some(Some(_))) { | |
| 1332 | let key = (format, program.normalized()); | |
| 1333 | if !self.ensure_compiled_key(device, &key) { | |
| 1281 | 1334 | return false; |
| 1282 | 1335 | } |
| 1283 | 1336 | |
| 1284 | let offset = self.alloc_uniform_slot(device, queue, params); | |
| 1285 | let (bg_hash, bg_idx) = self.ensure_bind_group(device, format, src_1, src_2, src_3, mask, dst_view); | |
| 1286 | let pipeline = self.pipelines[&key].as_ref().expect("pipeline checked above"); | |
| 1337 | let (chunk_idx, offset) = self.alloc_uniform_slot(device, queue, params); | |
| 1338 | let (bg_hash, bg_idx) = self.ensure_bind_group( | |
| 1339 | device, | |
| 1340 | format, | |
| 1341 | src_1, | |
| 1342 | src_2, | |
| 1343 | src_3, | |
| 1344 | mask, | |
| 1345 | dst_view, | |
| 1346 | chunk_idx, | |
| 1347 | ); | |
| 1348 | let Some(Some(pipeline)) = self.pipelines.get(&key).map(|e| &e.pipeline) else { | |
| 1349 | return false; | |
| 1350 | }; | |
| 1287 | 1351 | let bg = &self.bg_cache[&bg_hash][bg_idx].bg; |
| 1288 | 1352 | |
| 1289 | 1353 | let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { |
| ... | ... | @@ -1706,4 +1770,141 @@ mod tests { |
| 1706 | 1770 | eng.queue().submit(Some(enc.finish())); |
| 1707 | 1771 | assert_eq!(cache.cached_bind_group_count(), CUSTOM_BIND_GROUP_CACHE_CAPACITY); |
| 1708 | 1772 | } |
| 1773 | ||
| 1774 | #[test] | |
| 1775 | fn custom_shader_cache_grows_uniform_chunks_past_256_without_wrapping() { | |
| 1776 | let eng = Engine::new_headless().expect("gpu"); | |
| 1777 | let mut cache = CustomShaderCache::new(); | |
| 1778 | let prog = CustomProgram { | |
| 1779 | red: "n1".to_owned(), | |
| 1780 | green: "n2".to_owned(), | |
| 1781 | blue: "0".to_owned(), | |
| 1782 | alpha: "1".to_owned(), | |
| 1783 | ..Default::default() | |
| 1784 | }; | |
| 1785 | let dummy = create_texture(eng.device(), [1, 1], 0); | |
| 1786 | let dst_first = create_texture(eng.device(), [4, 4], 1); | |
| 1787 | let dst_mid = create_texture(eng.device(), [4, 4], 2); | |
| 1788 | let dst_last = create_texture(eng.device(), [4, 4], 3); | |
| 1789 | ||
| 1790 | let mut enc = eng.device().create_command_encoder(&Default::default()); | |
| 1791 | let first_params = CustomParams { | |
| 1792 | n0: [0.2, 0.8, 0.0, 0.0], | |
| 1793 | img_dims1: [4.0, 4.0, 4.0, 4.0], | |
| 1794 | ..Default::default() | |
| 1795 | }; | |
| 1796 | assert!(cache.dispatch( | |
| 1797 | eng.device(), | |
| 1798 | eng.queue(), | |
| 1799 | &mut enc, | |
| 1800 | &prog, | |
| 1801 | wgpu::TextureFormat::Rgba8Unorm, | |
| 1802 | &dummy.view, | |
| 1803 | &dummy.view, | |
| 1804 | &dummy.view, | |
| 1805 | &dummy.view, | |
| 1806 | &dst_first.view, | |
| 1807 | [4, 4], | |
| 1808 | &first_params, | |
| 1809 | )); | |
| 1810 | ||
| 1811 | // 260 intermediate dispatches push past slot 256 (into chunk 1) in a single unsubmitted encoder. | |
| 1812 | let fill_params = CustomParams { | |
| 1813 | n0: [0.9, 0.1, 0.0, 0.0], | |
| 1814 | img_dims1: [4.0, 4.0, 4.0, 4.0], | |
| 1815 | ..Default::default() | |
| 1816 | }; | |
| 1817 | for _ in 0..260 { | |
| 1818 | assert!(cache.dispatch( | |
| 1819 | eng.device(), | |
| 1820 | eng.queue(), | |
| 1821 | &mut enc, | |
| 1822 | &prog, | |
| 1823 | wgpu::TextureFormat::Rgba8Unorm, | |
| 1824 | &dummy.view, | |
| 1825 | &dummy.view, | |
| 1826 | &dummy.view, | |
| 1827 | &dummy.view, | |
| 1828 | &dst_mid.view, | |
| 1829 | [4, 4], | |
| 1830 | &fill_params, | |
| 1831 | )); | |
| 1832 | } | |
| 1833 | ||
| 1834 | let last_params = CustomParams { | |
| 1835 | n0: [0.6, 0.4, 0.0, 0.0], | |
| 1836 | img_dims1: [4.0, 4.0, 4.0, 4.0], | |
| 1837 | ..Default::default() | |
| 1838 | }; | |
| 1839 | assert!(cache.dispatch( | |
| 1840 | eng.device(), | |
| 1841 | eng.queue(), | |
| 1842 | &mut enc, | |
| 1843 | &prog, | |
| 1844 | wgpu::TextureFormat::Rgba8Unorm, | |
| 1845 | &dummy.view, | |
| 1846 | &dummy.view, | |
| 1847 | &dummy.view, | |
| 1848 | &dummy.view, | |
| 1849 | &dst_last.view, | |
| 1850 | [4, 4], | |
| 1851 | &last_params, | |
| 1852 | )); | |
| 1853 | ||
| 1854 | assert_eq!(cache.uniform_chunks.len(), 2); | |
| 1855 | assert_eq!(cache.uniform_cursor, 262); | |
| 1856 | assert!(cache.cached_bind_group_count() >= 2); | |
| 1857 | eng.queue().submit(Some(enc.finish())); | |
| 1858 | ||
| 1859 | let rgba_first = eng.readback_rgba(&dst_first).expect("readback first"); | |
| 1860 | let rgba_last = eng.readback_rgba(&dst_last).expect("readback last"); | |
| 1861 | assert!( | |
| 1862 | (rgba_first[0] as i32 - 51).abs() <= 1 && (rgba_first[1] as i32 - 204).abs() <= 1, | |
| 1863 | "slot 0 must not be overwritten by chunk 1 writes: got {:?}", | |
| 1864 | &rgba_first[..4] | |
| 1865 | ); | |
| 1866 | assert!( | |
| 1867 | (rgba_last[0] as i32 - 153).abs() <= 1 && (rgba_last[1] as i32 - 102).abs() <= 1, | |
| 1868 | "chunk 1 write must produce exact params: got {:?}", | |
| 1869 | &rgba_last[..4] | |
| 1870 | ); | |
| 1871 | ||
| 1872 | // Trimming chunks resets cursor and invalidates bind groups referencing trimmed buffers. | |
| 1873 | cache.trim_uniform_chunks(1); | |
| 1874 | assert_eq!(cache.uniform_chunks.len(), 1); | |
| 1875 | assert_eq!(cache.uniform_cursor, 0); | |
| 1876 | assert_eq!(cache.cached_bind_group_count(), 0); | |
| 1877 | } | |
| 1878 | ||
| 1879 | #[test] | |
| 1880 | fn custom_shader_cache_evicts_lru_pipelines_past_capacity() { | |
| 1881 | let eng = Engine::new_headless().expect("gpu"); | |
| 1882 | let mut cache = CustomShaderCache::new(); | |
| 1883 | let fmt = wgpu::TextureFormat::Rgba8Unorm; | |
| 1884 | let make_prog = |i: usize| CustomProgram { | |
| 1885 | red: format!("{i}.0 / 255.0"), | |
| 1886 | green: "0".to_owned(), | |
| 1887 | blue: "0".to_owned(), | |
| 1888 | alpha: "1".to_owned(), | |
| 1889 | ..Default::default() | |
| 1890 | }; | |
| 1891 | ||
| 1892 | for i in 0..MAX_CUSTOM_PIPELINES { | |
| 1893 | let p = make_prog(i); | |
| 1894 | assert!(cache.ensure_compiled(eng.device(), &p, fmt)); | |
| 1895 | } | |
| 1896 | assert_eq!(cache.cached_pipeline_count(), MAX_CUSTOM_PIPELINES); | |
| 1897 | ||
| 1898 | // Touch entry 0 so entry 1 becomes the least recently used pipeline. | |
| 1899 | let p0 = make_prog(0); | |
| 1900 | let p1 = make_prog(1); | |
| 1901 | assert!(cache.ensure_compiled(eng.device(), &p0, fmt)); | |
| 1902 | ||
| 1903 | let p_new = make_prog(MAX_CUSTOM_PIPELINES); | |
| 1904 | assert!(cache.ensure_compiled(eng.device(), &p_new, fmt)); | |
| 1905 | assert_eq!(cache.cached_pipeline_count(), MAX_CUSTOM_PIPELINES); | |
| 1906 | assert!(cache.pipelines.contains_key(&(fmt, p0.normalized()))); | |
| 1907 | assert!(!cache.pipelines.contains_key(&(fmt, p1.normalized()))); | |
| 1908 | assert!(cache.pipelines.contains_key(&(fmt, p_new.normalized()))); | |
| 1909 | } | |
| 1709 | 1910 | } |
crates/engine/src/eval/mod.rs+2| ... | ... | @@ -326,6 +326,7 @@ impl Engine { |
| 326 | 326 | self.sync_node_cache_budget(); |
| 327 | 327 | self.cache.bust_all(); |
| 328 | 328 | self.custom_shaders.clear_bind_group_cache(); |
| 329 | self.custom_shaders.trim_uniform_chunks(2); | |
| 329 | 330 | self.clear_bind_group_caches(); |
| 330 | 331 | self.trim_uniform_chunks(); |
| 331 | 332 | if let Some(of) = self.optflow.get() { |
| ... | ... | @@ -348,6 +349,7 @@ impl Engine { |
| 348 | 349 | self.sync_node_cache_budget(); |
| 349 | 350 | self.cache.bust_all(); |
| 350 | 351 | self.custom_shaders.clear_bind_group_cache(); |
| 352 | self.custom_shaders.trim_uniform_chunks(2); | |
| 351 | 353 | self.clear_bind_group_caches(); |
| 352 | 354 | self.trim_uniform_chunks(); |
| 353 | 355 | if let Some(of) = self.optflow.get() { |
crates/engine/src/eval/output_cache.rs+36-1| ... | ... | @@ -145,12 +145,15 @@ impl Engine { |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /// Total resident GPU VRAM bytes across `NodeCache` (live + pooled), unshared `output_cache` |
| 148 | /// entries, and pooled 3D shadow render targets. | |
| 148 | /// entries, 3D shadow/depth/mesh buffers, and optical flow scratch/cache buffers. | |
| 149 | 149 | pub fn vram_bytes(&self) -> u64 { |
| 150 | 150 | self.cache |
| 151 | 151 | .total_vram_bytes() |
| 152 | 152 | .saturating_add(self.output_cache.unshared_bytes()) |
| 153 | 153 | .saturating_add(self.scene3d.shadow_vram_bytes()) |
| 154 | .saturating_add(self.scene3d.depth_vram_bytes()) | |
| 155 | .saturating_add(self.scene3d.mesh_vram_bytes()) | |
| 156 | .saturating_add(self.optflow.get().map_or(0, |of| of.vram_bytes())) | |
| 154 | 157 | } |
| 155 | 158 | |
| 156 | 159 | pub fn vram_mb(&self) -> f64 { |
| ... | ... | @@ -231,4 +234,36 @@ mod tests { |
| 231 | 234 | drop(tex); |
| 232 | 235 | assert_eq!(cache.unshared_bytes(), tex_bytes + other_bytes); |
| 233 | 236 | } |
| 237 | ||
| 238 | #[test] | |
| 239 | fn vram_bytes_includes_optflow_and_bust_caches_clears_scratch() { | |
| 240 | let mut eng = Engine::new_headless().expect("gpu"); | |
| 241 | assert_eq!(eng.vram_bytes(), 0); | |
| 242 | ||
| 243 | let a = eng.alloc_tex([64, 64], BitDepth::Int8, 10); | |
| 244 | let b = eng.alloc_tex([64, 64], BitDepth::Int8, 11); | |
| 245 | let optflow = eng | |
| 246 | .optflow | |
| 247 | .get_or_init(|| crate::optflow::OpticalFlowPipelines::new(&eng.device)); | |
| 248 | let mut enc = eng.device.create_command_encoder(&Default::default()); | |
| 249 | let _out = optflow.interpolate_flow( | |
| 250 | &eng.device, | |
| 251 | &eng.queue, | |
| 252 | &mut enc, | |
| 253 | &a, | |
| 254 | &b, | |
| 255 | 0.5, | |
| 256 | wgpu::TextureFormat::Rgba8Unorm, | |
| 257 | ); | |
| 258 | eng.queue.submit(Some(enc.finish())); | |
| 259 | ||
| 260 | let optflow_bytes = eng.optflow.get().unwrap().vram_bytes(); | |
| 261 | assert!(optflow_bytes > 0); | |
| 262 | assert_eq!(eng.vram_bytes(), optflow_bytes); | |
| 263 | ||
| 264 | eng.bust_gpu_cache(); | |
| 265 | let after_bust = eng.optflow.get().unwrap().vram_bytes(); | |
| 266 | assert!(after_bust < optflow_bytes); | |
| 267 | assert_eq!(eng.vram_bytes(), after_bust); | |
| 268 | } | |
| 234 | 269 | } |
crates/engine/src/optflow.rs+212-54| ... | ... | @@ -455,8 +455,6 @@ const FLOW_CACHE_CAP: usize = 4; |
| 455 | 455 | |
| 456 | 456 | /// Uniform slots allocated per ring chunk (`256 * 256 B = 64 KiB` at 256-byte alignment). |
| 457 | 457 | const UNIFORM_SLOTS_PER_CHUNK: u32 = 256; |
| 458 | /// Maximum uniform ring chunks before wrapping (`32 * 256 = 8,192` slots, `<= 2 MiB`). | |
| 459 | const MAX_UNIFORM_CHUNKS: u32 = 32; | |
| 460 | 458 | |
| 461 | 459 | /// Maximum number of idle `Rgba16Float` pyramid textures retained in [`PyramidScratchPool`]. |
| 462 | 460 | const PYRAMID_POOL_MAX_TEXTURES: usize = 32; |
| ... | ... | @@ -485,14 +483,15 @@ impl UniformRing { |
| 485 | 483 | } |
| 486 | 484 | |
| 487 | 485 | /// Writes `uniform` into the next aligned dynamic-offset slot and returns `(chunk_buffer, offset)`. |
| 486 | /// | |
| 487 | /// Lazily appends chunks without wrapping mid-encoder; callers reset the cursor after submit. | |
| 488 | 488 | fn alloc_slot( |
| 489 | 489 | &mut self, |
| 490 | 490 | device: &wgpu::Device, |
| 491 | 491 | queue: &wgpu::Queue, |
| 492 | 492 | uniform: &OptFlowUniform, |
| 493 | 493 | ) -> (wgpu::Buffer, u32) { |
| 494 | let max_slots = UNIFORM_SLOTS_PER_CHUNK * MAX_UNIFORM_CHUNKS; | |
| 495 | let slot_global = self.cursor % max_slots; | |
| 494 | let slot_global = self.cursor; | |
| 496 | 495 | let chunk_idx = (slot_global / UNIFORM_SLOTS_PER_CHUNK) as usize; |
| 497 | 496 | let slot_in_chunk = slot_global % UNIFORM_SLOTS_PER_CHUNK; |
| 498 | 497 | while chunk_idx >= self.chunks.len() { |
| ... | ... | @@ -511,13 +510,24 @@ impl UniformRing { |
| 511 | 510 | u64::from(offset), |
| 512 | 511 | bytemuck::bytes_of(uniform), |
| 513 | 512 | ); |
| 514 | self.cursor = (slot_global + 1) % max_slots; | |
| 513 | self.cursor = slot_global + 1; | |
| 515 | 514 | (self.chunks[chunk_idx].clone(), offset) |
| 516 | 515 | } |
| 517 | 516 | |
| 518 | 517 | fn reset_cursor(&mut self) { |
| 519 | 518 | self.cursor = 0; |
| 520 | 519 | } |
| 520 | ||
| 521 | fn trim_uniform_chunks(&mut self, keep: usize) { | |
| 522 | self.cursor = 0; | |
| 523 | if self.chunks.len() > keep { | |
| 524 | self.chunks.truncate(keep); | |
| 525 | } | |
| 526 | } | |
| 527 | ||
| 528 | fn vram_bytes(&self) -> u64 { | |
| 529 | self.chunks.iter().map(wgpu::Buffer::size).sum() | |
| 530 | } | |
| 521 | 531 | } |
| 522 | 532 | |
| 523 | 533 | struct PooledF16Tex { |
| ... | ... | @@ -699,11 +709,28 @@ impl OpticalFlowPipelines { |
| 699 | 709 | self.uniform_ring.lock().unwrap().reset_cursor(); |
| 700 | 710 | } |
| 701 | 711 | |
| 702 | /// Clears cached flow fields and pooled intermediate pyramid textures, and resets the uniform cursor. | |
| 712 | /// Clears cached flow fields and pooled intermediate pyramid textures, and trims uniform ring chunks. | |
| 703 | 713 | pub fn clear_scratch(&self) { |
| 704 | 714 | self.flow_cache.lock().unwrap().clear(); |
| 705 | 715 | self.pyramid_pool.lock().unwrap().clear(); |
| 706 | self.uniform_ring.lock().unwrap().reset_cursor(); | |
| 716 | self.uniform_ring.lock().unwrap().trim_uniform_chunks(2); | |
| 717 | } | |
| 718 | ||
| 719 | /// Total resident GPU VRAM bytes currently held across pooled pyramid textures, | |
| 720 | /// cached flow-field textures, and dynamic uniform ring buffers. | |
| 721 | pub fn vram_bytes(&self) -> u64 { | |
| 722 | let pool_bytes = self.pyramid_pool.lock().unwrap().total_bytes; | |
| 723 | let flow_bytes: u64 = self | |
| 724 | .flow_cache | |
| 725 | .lock() | |
| 726 | .unwrap() | |
| 727 | .iter() | |
| 728 | .map(|(_, _, _, size)| u64::from(size[0].max(1)) * u64::from(size[1].max(1)) * 8) | |
| 729 | .sum(); | |
| 730 | let ring_bytes = self.uniform_ring.lock().unwrap().vram_bytes(); | |
| 731 | pool_bytes | |
| 732 | .saturating_add(flow_bytes) | |
| 733 | .saturating_add(ring_bytes) | |
| 707 | 734 | } |
| 708 | 735 | |
| 709 | 736 | fn ensure_target_pipeline( |
| ... | ... | @@ -853,51 +880,59 @@ impl OpticalFlowPipelines { |
| 853 | 880 | |
| 854 | 881 | let s0 = [frame_a.size[0].max(1), frame_a.size[1].max(1)]; |
| 855 | 882 | let s1 = half_size(s0); |
| 856 | let flow_key = (frame_a.content_hash, frame_b.content_hash, s0); | |
| 857 | let cached = { | |
| 858 | let mut cache = self.flow_cache.lock().unwrap(); | |
| 859 | cache.iter().position(|(k, _, _, _)| *k == flow_key).map(|i| { | |
| 860 | let entry = cache.remove(i); | |
| 861 | let view = entry.1.clone(); | |
| 862 | cache.push(entry); | |
| 863 | view | |
| 864 | }) | |
| 865 | }; | |
| 866 | let flow1_view = match cached { | |
| 867 | Some(view) => view, | |
| 868 | None => { | |
| 869 | // Evict the LRU flow field before computing the new one so its backing | |
| 870 | // texture can be immediately reused by PyramidScratchPool. | |
| 871 | let evicted_before = { | |
| 872 | let mut cache = self.flow_cache.lock().unwrap(); | |
| 873 | if cache.len() >= FLOW_CACHE_CAP { | |
| 874 | Some(cache.remove(0)) | |
| 875 | } else { | |
| 876 | None | |
| 883 | let unkeyed = frame_a.content_hash == 0 || frame_b.content_hash == 0; | |
| 884 | let (flow1_view, uncached_flow): (wgpu::TextureView, Option<wgpu::Texture>) = if unkeyed { | |
| 885 | let (flow1_tex, flow1_view) = | |
| 886 | self.compute_flow_field(device, queue, enc, frame_a, frame_b); | |
| 887 | (flow1_view, Some(flow1_tex)) | |
| 888 | } else { | |
| 889 | let flow_key = (frame_a.content_hash, frame_b.content_hash, s0); | |
| 890 | let cached = { | |
| 891 | let mut cache = self.flow_cache.lock().unwrap(); | |
| 892 | cache.iter().position(|(k, _, _, _)| *k == flow_key).map(|i| { | |
| 893 | let entry = cache.remove(i); | |
| 894 | let view = entry.1.clone(); | |
| 895 | cache.push(entry); | |
| 896 | view | |
| 897 | }) | |
| 898 | }; | |
| 899 | let flow1_view = match cached { | |
| 900 | Some(view) => view, | |
| 901 | None => { | |
| 902 | // Evict the LRU flow field before computing the new one so its backing | |
| 903 | // texture can be immediately reused by PyramidScratchPool. | |
| 904 | let evicted_before = { | |
| 905 | let mut cache = self.flow_cache.lock().unwrap(); | |
| 906 | if cache.len() >= FLOW_CACHE_CAP { | |
| 907 | Some(cache.remove(0)) | |
| 908 | } else { | |
| 909 | None | |
| 910 | } | |
| 911 | }; | |
| 912 | if let Some((_, old_view, old_tex, old_size)) = evicted_before { | |
| 913 | self.pyramid_pool.lock().unwrap().release(old_tex, old_view, old_size); | |
| 877 | 914 | } |
| 878 | }; | |
| 879 | if let Some((_, old_view, old_tex, old_size)) = evicted_before { | |
| 880 | self.pyramid_pool.lock().unwrap().release(old_tex, old_view, old_size); | |
| 881 | } | |
| 882 | 915 | |
| 883 | let (flow1_tex, flow1_view) = | |
| 884 | self.compute_flow_field(device, queue, enc, frame_a, frame_b); | |
| 885 | ||
| 886 | let evicted_after = { | |
| 887 | let mut cache = self.flow_cache.lock().unwrap(); | |
| 888 | let extra = if cache.len() >= FLOW_CACHE_CAP { | |
| 889 | Some(cache.remove(0)) | |
| 890 | } else { | |
| 891 | None | |
| 916 | let (flow1_tex, flow1_view) = | |
| 917 | self.compute_flow_field(device, queue, enc, frame_a, frame_b); | |
| 918 | ||
| 919 | let evicted_after = { | |
| 920 | let mut cache = self.flow_cache.lock().unwrap(); | |
| 921 | let extra = if cache.len() >= FLOW_CACHE_CAP { | |
| 922 | Some(cache.remove(0)) | |
| 923 | } else { | |
| 924 | None | |
| 925 | }; | |
| 926 | cache.push((flow_key, flow1_view.clone(), flow1_tex, s1)); | |
| 927 | extra | |
| 892 | 928 | }; |
| 893 | cache.push((flow_key, flow1_view.clone(), flow1_tex, s1)); | |
| 894 | extra | |
| 895 | }; | |
| 896 | if let Some((_, old_view, old_tex, old_size)) = evicted_after { | |
| 897 | self.pyramid_pool.lock().unwrap().release(old_tex, old_view, old_size); | |
| 929 | if let Some((_, old_view, old_tex, old_size)) = evicted_after { | |
| 930 | self.pyramid_pool.lock().unwrap().release(old_tex, old_view, old_size); | |
| 931 | } | |
| 932 | flow1_view | |
| 898 | 933 | } |
| 899 | flow1_view | |
| 900 | } | |
| 934 | }; | |
| 935 | (flow1_view, None) | |
| 901 | 936 | }; |
| 902 | 937 | |
| 903 | 938 | // Full-resolution bidirectional occlusion-aware warp & blend |
| ... | ... | @@ -907,12 +942,17 @@ impl OpticalFlowPipelines { |
| 907 | 942 | alpha.clamp(0.0, 1.0), |
| 908 | 943 | 0.0, |
| 909 | 944 | ]; |
| 910 | let guard = self.warp_pipelines.read().unwrap(); | |
| 911 | let pipe = guard.get(&target_format).unwrap(); | |
| 912 | self.run_pass( | |
| 913 | device, queue, enc, pipe, "optflow-warp", | |
| 914 | &frame_a.view, &frame_b.view, &flow1_view, &dst.view, s0, scale_1_to_0, | |
| 915 | ); | |
| 945 | { | |
| 946 | let guard = self.warp_pipelines.read().unwrap(); | |
| 947 | let pipe = guard.get(&target_format).unwrap(); | |
| 948 | self.run_pass( | |
| 949 | device, queue, enc, pipe, "optflow-warp", | |
| 950 | &frame_a.view, &frame_b.view, &flow1_view, &dst.view, s0, scale_1_to_0, | |
| 951 | ); | |
| 952 | } | |
| 953 | if let Some(flow1_tex) = uncached_flow { | |
| 954 | self.pyramid_pool.lock().unwrap().release(flow1_tex, flow1_view, s1); | |
| 955 | } | |
| 916 | 956 | dst |
| 917 | 957 | } |
| 918 | 958 | |
| ... | ... | @@ -1759,6 +1799,124 @@ mod tests { |
| 1759 | 1799 | assert_eq!(flow_last_px, readback_bytes(&device, &queue, &ref_last, 4)); |
| 1760 | 1800 | } |
| 1761 | 1801 | |
| 1802 | #[test] | |
| 1803 | fn test_unkeyed_content_hash_zero_bypasses_flow_cache_and_releases_flow1() { | |
| 1804 | let (device, queue) = create_headless_device(); | |
| 1805 | let pipelines = OpticalFlowPipelines::new(&device); | |
| 1806 | let (w, h) = (96u32, 64u32); | |
| 1807 | let fmt = wgpu::TextureFormat::Rgba8Unorm; | |
| 1808 | ||
| 1809 | // Pair 1: both content_hash == 0 (unkeyed scratch textures). | |
| 1810 | let a1 = upload_rgba8(&device, &queue, w, h, &render_box_scene(w, h, 8, 8), 0); | |
| 1811 | let b1 = upload_rgba8(&device, &queue, w, h, &render_box_scene(w, h, 24, 8), 0); | |
| 1812 | let mut enc1 = device.create_command_encoder(&Default::default()); | |
| 1813 | let _out1 = pipelines.interpolate_flow(&device, &queue, &mut enc1, &a1, &b1, 0.5, fmt); | |
| 1814 | queue.submit(Some(enc1.finish())); | |
| 1815 | assert!( | |
| 1816 | pipelines.flow_cache.lock().unwrap().is_empty(), | |
| 1817 | "content_hash == 0 must bypass flow_cache" | |
| 1818 | ); | |
| 1819 | // All 14 textures (13 intermediate + temporary flow1) must be returned to pyramid_pool. | |
| 1820 | assert_eq!(pipelines.pyramid_pool.lock().unwrap().entries.len(), 14); | |
| 1821 | ||
| 1822 | // Pair 2: completely different box positions, also with content_hash == 0 (or one side 0). | |
| 1823 | let a2 = upload_rgba8(&device, &queue, w, h, &render_box_scene(w, h, 40, 12), 0); | |
| 1824 | let b2 = upload_rgba8(&device, &queue, w, h, &render_box_scene(w, h, 40, 28), 99); | |
| 1825 | let mut enc2 = device.create_command_encoder(&Default::default()); | |
| 1826 | let out2 = pipelines.interpolate_flow(&device, &queue, &mut enc2, &a2, &b2, 0.5, fmt); | |
| 1827 | queue.submit(Some(enc2.finish())); | |
| 1828 | assert!(pipelines.flow_cache.lock().unwrap().is_empty()); | |
| 1829 | ||
| 1830 | let fresh = OpticalFlowPipelines::new(&device); | |
| 1831 | let mut fresh_enc = device.create_command_encoder(&Default::default()); | |
| 1832 | let fresh_out = fresh.interpolate_flow(&device, &queue, &mut fresh_enc, &a2, &b2, 0.5, fmt); | |
| 1833 | queue.submit(Some(fresh_enc.finish())); | |
| 1834 | assert_eq!( | |
| 1835 | readback_bytes(&device, &queue, &out2, 4), | |
| 1836 | readback_bytes(&device, &queue, &fresh_out, 4), | |
| 1837 | "unkeyed flow computation must not alias any stale cached flow field" | |
| 1838 | ); | |
| 1839 | } | |
| 1840 | ||
| 1841 | #[test] | |
| 1842 | fn test_optflow_vram_bytes_tracks_memory_and_clears() { | |
| 1843 | let (device, queue) = create_headless_device(); | |
| 1844 | let pipelines = OpticalFlowPipelines::new(&device); | |
| 1845 | assert_eq!(pipelines.vram_bytes(), 0); | |
| 1846 | ||
| 1847 | let (w, h) = (128u32, 64u32); | |
| 1848 | let a = upload_rgba8(&device, &queue, w, h, &render_box_scene(w, h, 16, 16), 11); | |
| 1849 | let b = upload_rgba8(&device, &queue, w, h, &render_box_scene(w, h, 32, 16), 12); | |
| 1850 | let mut enc = device.create_command_encoder(&Default::default()); | |
| 1851 | let _out = pipelines.interpolate_flow( | |
| 1852 | &device, | |
| 1853 | &queue, | |
| 1854 | &mut enc, | |
| 1855 | &a, | |
| 1856 | &b, | |
| 1857 | 0.5, | |
| 1858 | wgpu::TextureFormat::Rgba8Unorm, | |
| 1859 | ); | |
| 1860 | queue.submit(Some(enc.finish())); | |
| 1861 | ||
| 1862 | let pool_bytes = pipelines.pyramid_pool.lock().unwrap().total_bytes; | |
| 1863 | let flow_bytes = u64::from(w / 2) * u64::from(h / 2) * 8; | |
| 1864 | let ring_bytes = pipelines.uniform_ring.lock().unwrap().vram_bytes(); | |
| 1865 | assert!(pool_bytes > 0); | |
| 1866 | assert!(ring_bytes > 0); | |
| 1867 | assert_eq!(pipelines.vram_bytes(), pool_bytes + flow_bytes + ring_bytes); | |
| 1868 | ||
| 1869 | // Grow uniform ring beyond 2 chunks so clear_scratch() exercises trim_uniform_chunks(2). | |
| 1870 | { | |
| 1871 | let mut ring = pipelines.uniform_ring.lock().unwrap(); | |
| 1872 | let dummy_u = OptFlowUniform { | |
| 1873 | dst_size: [1.0; 4], | |
| 1874 | params: [0.0; 4], | |
| 1875 | }; | |
| 1876 | for _ in 0..(UNIFORM_SLOTS_PER_CHUNK * 3) { | |
| 1877 | ring.alloc_slot(&device, &queue, &dummy_u); | |
| 1878 | } | |
| 1879 | assert!(ring.chunks.len() >= 3); | |
| 1880 | } | |
| 1881 | assert!(pipelines.vram_bytes() > pool_bytes + flow_bytes + ring_bytes); | |
| 1882 | ||
| 1883 | pipelines.clear_scratch(); | |
| 1884 | assert_eq!(pipelines.pyramid_pool.lock().unwrap().total_bytes, 0); | |
| 1885 | assert!(pipelines.flow_cache.lock().unwrap().is_empty()); | |
| 1886 | assert_eq!(pipelines.uniform_ring.lock().unwrap().chunks.len(), 2); | |
| 1887 | let retained_ring = pipelines.uniform_ring.lock().unwrap().vram_bytes(); | |
| 1888 | assert_eq!(pipelines.vram_bytes(), retained_ring); | |
| 1889 | ||
| 1890 | pipelines.uniform_ring.lock().unwrap().trim_uniform_chunks(0); | |
| 1891 | assert_eq!(pipelines.vram_bytes(), 0); | |
| 1892 | } | |
| 1893 | ||
| 1894 | #[test] | |
| 1895 | fn test_optflow_uniform_ring_grows_dynamically_past_8192_slots_without_wrapping() { | |
| 1896 | let (device, queue) = create_headless_device(); | |
| 1897 | let mut ring = UniformRing::new(&device); | |
| 1898 | let dummy_u = OptFlowUniform { | |
| 1899 | dst_size: [64.0, 64.0, 1.0 / 64.0, 1.0 / 64.0], | |
| 1900 | params: [1.0, 1.0, 0.5, 0.0], | |
| 1901 | }; | |
| 1902 | ||
| 1903 | // 32 * 256 = 8,192 was the old modulo cap; allocate 8,197 slots without resetting cursor. | |
| 1904 | let total = 32 * UNIFORM_SLOTS_PER_CHUNK + 5; | |
| 1905 | let mut last = None; | |
| 1906 | for _ in 0..total { | |
| 1907 | last = Some(ring.alloc_slot(&device, &queue, &dummy_u)); | |
| 1908 | } | |
| 1909 | let (last_buf, last_offset) = last.expect("allocated slots"); | |
| 1910 | assert_eq!(ring.chunks.len(), 33, "must allocate chunk 32 instead of wrapping to 0"); | |
| 1911 | assert_eq!(ring.cursor, total); | |
| 1912 | assert_eq!(last_buf, ring.chunks[32]); | |
| 1913 | assert_eq!(last_offset, 4 * ring.stride); | |
| 1914 | ||
| 1915 | ring.trim_uniform_chunks(2); | |
| 1916 | assert_eq!(ring.chunks.len(), 2); | |
| 1917 | assert_eq!(ring.cursor, 0); | |
| 1918 | } | |
| 1919 | ||
| 1762 | 1920 | #[test] |
| 1763 | 1921 | #[ignore] |
| 1764 | 1922 | fn bench_optflow_10_frames_720p_1080p() { |