| ... | @@ -900,14 +900,20 @@ fn custom_tool_op(@builtin(global_invocation_id) gid: vec3<u32>) {{ | ... | @@ -900,14 +900,20 @@ fn custom_tool_op(@builtin(global_invocation_id) gid: vec3<u32>) {{ |
| 900 | // Pipeline Cache & GPU Dispatch | 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 | const CUSTOM_BIND_GROUP_CACHE_CAPACITY: usize = 128; | 904 | const CUSTOM_BIND_GROUP_CACHE_CAPACITY: usize = 128; |
| | 905 | const MAX_CUSTOM_PIPELINES: usize = 128; |
| 905 | | 906 | |
| 906 | struct FormatLayouts { | 907 | struct FormatLayouts { |
| 907 | bgl: wgpu::BindGroupLayout, | 908 | bgl: wgpu::BindGroupLayout, |
| 908 | pl: wgpu::PipelineLayout, | 909 | pl: wgpu::PipelineLayout, |
| 909 | } | 910 | } |
| 910 | | 911 | |
| | 912 | struct CustomPipelineEntry { |
| | 913 | pipeline: Option<wgpu::ComputePipeline>, |
| | 914 | last_used: u64, |
| | 915 | } |
| | 916 | |
| 911 | struct CustomBindGroupKey { | 917 | struct CustomBindGroupKey { |
| 912 | format: wgpu::TextureFormat, | 918 | format: wgpu::TextureFormat, |
| 913 | src_1: wgpu::TextureView, | 919 | src_1: wgpu::TextureView, |
| ... | @@ -915,9 +921,11 @@ struct CustomBindGroupKey { | ... | @@ -915,9 +921,11 @@ struct CustomBindGroupKey { |
| 915 | src_3: wgpu::TextureView, | 921 | src_3: wgpu::TextureView, |
| 916 | mask: wgpu::TextureView, | 922 | mask: wgpu::TextureView, |
| 917 | dst_view: wgpu::TextureView, | 923 | dst_view: wgpu::TextureView, |
| | 924 | chunk_idx: usize, |
| 918 | } | 925 | } |
| 919 | | 926 | |
| 920 | impl CustomBindGroupKey { | 927 | impl CustomBindGroupKey { |
| | 928 | #[allow(clippy::too_many_arguments)] |
| 921 | fn matches( | 929 | fn matches( |
| 922 | &self, | 930 | &self, |
| 923 | format: wgpu::TextureFormat, | 931 | format: wgpu::TextureFormat, |
| ... | @@ -926,6 +934,7 @@ impl CustomBindGroupKey { | ... | @@ -926,6 +934,7 @@ impl CustomBindGroupKey { |
| 926 | src_3: &wgpu::TextureView, | 934 | src_3: &wgpu::TextureView, |
| 927 | mask: &wgpu::TextureView, | 935 | mask: &wgpu::TextureView, |
| 928 | dst_view: &wgpu::TextureView, | 936 | dst_view: &wgpu::TextureView, |
| | 937 | chunk_idx: usize, |
| 929 | ) -> bool { | 938 | ) -> bool { |
| 930 | self.format == format | 939 | self.format == format |
| 931 | && &self.src_1 == src_1 | 940 | && &self.src_1 == src_1 |
| ... | @@ -933,6 +942,7 @@ impl CustomBindGroupKey { | ... | @@ -933,6 +942,7 @@ impl CustomBindGroupKey { |
| 933 | && &self.src_3 == src_3 | 942 | && &self.src_3 == src_3 |
| 934 | && &self.mask == mask | 943 | && &self.mask == mask |
| 935 | && &self.dst_view == dst_view | 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,6 +959,7 @@ fn hash_custom_bind_group_key( |
| 949 | src_3: &wgpu::TextureView, | 959 | src_3: &wgpu::TextureView, |
| 950 | mask: &wgpu::TextureView, | 960 | mask: &wgpu::TextureView, |
| 951 | dst_view: &wgpu::TextureView, | 961 | dst_view: &wgpu::TextureView, |
| | 962 | chunk_idx: usize, |
| 952 | ) -> u64 { | 963 | ) -> u64 { |
| 953 | let mut h = DefaultHasher::new(); | 964 | let mut h = DefaultHasher::new(); |
| 954 | format.hash(&mut h); | 965 | format.hash(&mut h); |
| ... | @@ -957,19 +968,21 @@ fn hash_custom_bind_group_key( | ... | @@ -957,19 +968,21 @@ fn hash_custom_bind_group_key( |
| 957 | src_3.hash(&mut h); | 968 | src_3.hash(&mut h); |
| 958 | mask.hash(&mut h); | 969 | mask.hash(&mut h); |
| 959 | dst_view.hash(&mut h); | 970 | dst_view.hash(&mut h); |
| | 971 | chunk_idx.hash(&mut h); |
| 960 | h.finish() | 972 | h.finish() |
| 961 | } | 973 | } |
| 962 | | 974 | |
| 963 | /// Caches dynamically compiled `CustomTool` compute pipelines keyed by | 975 | /// Caches dynamically compiled `CustomTool` compute pipelines keyed by |
| 964 | /// `(dst_texture_format, hash_of_normalized_program)`, plus a persistent dynamic-offset | 976 | /// `(dst_texture_format, normalized_program)` with an LRU cap, plus a lazily growing |
| 965 | /// uniform ring buffer and LRU-cached `BindGroup`s so repeated dispatches avoid per-frame | 977 | /// chunked dynamic-offset uniform ring buffer and LRU-cached `BindGroup`s so repeated |
| 966 | /// GPU buffer/bind-group allocations. | 978 | /// dispatches avoid per-frame GPU buffer/bind-group allocations. |
| 967 | #[derive(Default)] | 979 | #[derive(Default)] |
| 968 | pub struct CustomShaderCache { | 980 | pub struct CustomShaderCache { |
| 969 | layouts: HashMap<wgpu::TextureFormat, FormatLayouts>, | 981 | layouts: HashMap<wgpu::TextureFormat, FormatLayouts>, |
| 970 | sampler: Option<wgpu::Sampler>, | 982 | sampler: Option<wgpu::Sampler>, |
| 971 | pipelines: HashMap<(wgpu::TextureFormat, u64), Option<wgpu::ComputePipeline>>, | 983 | pipelines: HashMap<(wgpu::TextureFormat, CustomProgram), CustomPipelineEntry>, |
| 972 | uniform_buf: Option<wgpu::Buffer>, | 984 | pipeline_tick: u64, |
| | 985 | uniform_chunks: Vec<wgpu::Buffer>, |
| 973 | uniform_stride: u32, | 986 | uniform_stride: u32, |
| 974 | uniform_cursor: u32, | 987 | uniform_cursor: u32, |
| 975 | bg_cache: HashMap<u64, Vec<CustomBindGroupEntry>>, | 988 | bg_cache: HashMap<u64, Vec<CustomBindGroupEntry>>, |
| ... | @@ -1009,6 +1022,16 @@ impl CustomShaderCache { | ... | @@ -1009,6 +1022,16 @@ impl CustomShaderCache { |
| 1009 | self.uniform_cursor = 0; | 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 | fn ensure_format_layouts(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat) { | 1035 | fn ensure_format_layouts(&mut self, device: &wgpu::Device, format: wgpu::TextureFormat) { |
| 1013 | self.layouts.entry(format).or_insert_with(|| { | 1036 | self.layouts.entry(format).or_insert_with(|| { |
| 1014 | let tex_entry = |binding| wgpu::BindGroupLayoutEntry { | 1037 | let tex_entry = |binding| wgpu::BindGroupLayoutEntry { |
| ... | @@ -1076,21 +1099,13 @@ impl CustomShaderCache { | ... | @@ -1076,21 +1099,13 @@ impl CustomShaderCache { |
| 1076 | } | 1099 | } |
| 1077 | } | 1100 | } |
| 1078 | | 1101 | |
| 1079 | fn ensure_uniform_buffer(&mut self, device: &wgpu::Device) { | 1102 | fn ensure_uniform_stride(&mut self, device: &wgpu::Device) { |
| 1080 | if self.uniform_buf.is_some() { | 1103 | if self.uniform_stride != 0 { |
| 1081 | return; | 1104 | return; |
| 1082 | } | 1105 | } |
| 1083 | let uniform_size = std::mem::size_of::<CustomParams>() as u32; | 1106 | let uniform_size = std::mem::size_of::<CustomParams>() as u32; |
| 1084 | let min_align = device.limits().min_uniform_buffer_offset_alignment.max(1); | 1107 | let min_align = device.limits().min_uniform_buffer_offset_alignment.max(1); |
| 1085 | let stride = uniform_size.div_ceil(min_align) * min_align; | 1108 | self.uniform_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); | | |
| 1094 | } | 1109 | } |
| 1095 | | 1110 | |
| 1096 | fn alloc_uniform_slot( | 1111 | fn alloc_uniform_slot( |
| ... | @@ -1098,14 +1113,29 @@ impl CustomShaderCache { | ... | @@ -1098,14 +1113,29 @@ impl CustomShaderCache { |
| 1098 | device: &wgpu::Device, | 1113 | device: &wgpu::Device, |
| 1099 | queue: &wgpu::Queue, | 1114 | queue: &wgpu::Queue, |
| 1100 | params: &CustomParams, | 1115 | params: &CustomParams, |
| 1101 | ) -> u32 { | 1116 | ) -> (usize, u32) { |
| 1102 | self.ensure_uniform_buffer(device); | 1117 | self.ensure_uniform_stride(device); |
| 1103 | let slot = self.uniform_cursor; | 1118 | let slot_global = self.uniform_cursor; |
| 1104 | let offset = slot * self.uniform_stride; | 1119 | let chunk_idx = (slot_global / CUSTOM_UNIFORM_SLOTS_PER_CHUNK) as usize; |
| 1105 | let ubuf = self.uniform_buf.as_ref().expect("uniform buffer initialized"); | 1120 | let slot_in_chunk = slot_global % CUSTOM_UNIFORM_SLOTS_PER_CHUNK; |
| 1106 | queue.write_buffer(ubuf, u64::from(offset), bytemuck::bytes_of(params)); | 1121 | while chunk_idx >= self.uniform_chunks.len() { |
| 1107 | self.uniform_cursor = (slot + 1) % CUSTOM_UNIFORM_SLOTS; | 1122 | let size = u64::from(CUSTOM_UNIFORM_SLOTS_PER_CHUNK) * u64::from(self.uniform_stride); |
| 1108 | offset | 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 | #[allow(clippy::too_many_arguments)] | 1141 | #[allow(clippy::too_many_arguments)] |
| ... | @@ -1118,8 +1148,9 @@ impl CustomShaderCache { | ... | @@ -1118,8 +1148,9 @@ impl CustomShaderCache { |
| 1118 | src_3: &wgpu::TextureView, | 1148 | src_3: &wgpu::TextureView, |
| 1119 | mask: &wgpu::TextureView, | 1149 | mask: &wgpu::TextureView, |
| 1120 | dst_view: &wgpu::TextureView, | 1150 | dst_view: &wgpu::TextureView, |
| | 1151 | chunk_idx: usize, |
| 1121 | ) -> (u64, usize) { | 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 | self.bg_tick = self.bg_tick.wrapping_add(1); | 1154 | self.bg_tick = self.bg_tick.wrapping_add(1); |
| 1124 | let tick = self.bg_tick; | 1155 | let tick = self.bg_tick; |
| 1125 | | 1156 | |
| ... | @@ -1127,7 +1158,7 @@ impl CustomShaderCache { | ... | @@ -1127,7 +1158,7 @@ impl CustomShaderCache { |
| 1127 | && let Some((idx, entry)) = bucket | 1158 | && let Some((idx, entry)) = bucket |
| 1128 | .iter_mut() | 1159 | .iter_mut() |
| 1129 | .enumerate() | 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 | entry.last_used = tick; | 1163 | entry.last_used = tick; |
| 1133 | self.bg_hits += 1; | 1164 | self.bg_hits += 1; |
| ... | @@ -1164,7 +1195,7 @@ impl CustomShaderCache { | ... | @@ -1164,7 +1195,7 @@ impl CustomShaderCache { |
| 1164 | | 1195 | |
| 1165 | let layout = &self.layouts[&format].bgl; | 1196 | let layout = &self.layouts[&format].bgl; |
| 1166 | let sampler = self.sampler.as_ref().expect("sampler initialized"); | 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 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { | 1199 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 1169 | label: Some("custom-tool-bg"), | 1200 | label: Some("custom-tool-bg"), |
| 1170 | layout, | 1201 | layout, |
| ... | @@ -1196,6 +1227,7 @@ impl CustomShaderCache { | ... | @@ -1196,6 +1227,7 @@ impl CustomShaderCache { |
| 1196 | src_3: src_3.clone(), | 1227 | src_3: src_3.clone(), |
| 1197 | mask: mask.clone(), | 1228 | mask: mask.clone(), |
| 1198 | dst_view: dst_view.clone(), | 1229 | dst_view: dst_view.clone(), |
| | 1230 | chunk_idx, |
| 1199 | }, | 1231 | }, |
| 1200 | bg, | 1232 | bg, |
| 1201 | last_used: tick, | 1233 | last_used: tick, |
| ... | @@ -1233,28 +1265,53 @@ impl CustomShaderCache { | ... | @@ -1233,28 +1265,53 @@ impl CustomShaderCache { |
| 1233 | Some(pipeline) | 1265 | Some(pipeline) |
| 1234 | } | 1266 | } |
| 1235 | | 1267 | |
| 1236 | /// Compiles and caches the compute pipeline for `program` at `format`, returning `false` | 1268 | fn ensure_compiled_key( |
| 1237 | /// if the program contains unsupported syntax (graceful fallback to passthrough). | | |
| 1238 | pub fn ensure_compiled( | | |
| 1239 | &mut self, | 1269 | &mut self, |
| 1240 | device: &wgpu::Device, | 1270 | device: &wgpu::Device, |
| 1241 | program: &CustomProgram, | 1271 | key: &(wgpu::TextureFormat, CustomProgram), |
| 1242 | format: wgpu::TextureFormat, | | |
| 1243 | ) -> bool { | 1272 | ) -> bool { |
| 1244 | let norm = program.normalized(); | 1273 | self.pipeline_tick = self.pipeline_tick.wrapping_add(1); |
| 1245 | let key = (format, norm.program_hash()); | 1274 | let tick = self.pipeline_tick; |
| 1246 | if let Some(entry) = self.pipelines.get(&key) { | 1275 | if let Some(entry) = self.pipelines.get_mut(key) { |
| 1247 | return entry.is_some(); | 1276 | entry.last_used = tick; |
| | 1277 | return entry.pipeline.is_some(); |
| 1248 | } | 1278 | } |
| 1249 | let pipeline = match compile_wgsl(&norm, format) { | 1279 | let pipeline = match compile_wgsl(&key.1, key.0) { |
| 1250 | Ok(wgsl) => self.compile_wgsl_pipeline(device, format, &wgsl), | 1280 | Ok(wgsl) => self.compile_wgsl_pipeline(device, key.0, &wgsl), |
| 1251 | Err(_) => None, | 1281 | Err(_) => None, |
| 1252 | }; | 1282 | }; |
| 1253 | let ok = pipeline.is_some(); | 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 | ok | 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 | /// Dispatches a compiled `CustomProgram` into `dst_view`. Returns `false` if compilation failed. | 1315 | /// Dispatches a compiled `CustomProgram` into `dst_view`. Returns `false` if compilation failed. |
| 1259 | #[allow(clippy::too_many_arguments)] | 1316 | #[allow(clippy::too_many_arguments)] |
| 1260 | pub fn dispatch( | 1317 | pub fn dispatch( |
| ... | @@ -1272,18 +1329,25 @@ impl CustomShaderCache { | ... | @@ -1272,18 +1329,25 @@ impl CustomShaderCache { |
| 1272 | dst_size: [u32; 2], | 1329 | dst_size: [u32; 2], |
| 1273 | params: &CustomParams, | 1330 | params: &CustomParams, |
| 1274 | ) -> bool { | 1331 | ) -> bool { |
| 1275 | let norm = program.normalized(); | 1332 | let key = (format, program.normalized()); |
| 1276 | let key = (format, norm.program_hash()); | 1333 | if !self.ensure_compiled_key(device, &key) { |
| 1277 | if !self.ensure_compiled(device, &norm, format) { | | |
| 1278 | return false; | | |
| 1279 | } | | |
| 1280 | if !matches!(self.pipelines.get(&key), Some(Some(_))) { | | |
| 1281 | return false; | 1334 | return false; |
| 1282 | } | 1335 | } |
| 1283 | | 1336 | |
| 1284 | let offset = self.alloc_uniform_slot(device, queue, params); | 1337 | let (chunk_idx, 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); | 1338 | let (bg_hash, bg_idx) = self.ensure_bind_group( |
| 1286 | let pipeline = self.pipelines[&key].as_ref().expect("pipeline checked above"); | 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 | let bg = &self.bg_cache[&bg_hash][bg_idx].bg; | 1351 | let bg = &self.bg_cache[&bg_hash][bg_idx].bg; |
| 1288 | | 1352 | |
| 1289 | let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { | 1353 | let mut pass = enc.begin_compute_pass(&wgpu::ComputePassDescriptor { |
| ... | @@ -1706,4 +1770,141 @@ mod tests { | ... | @@ -1706,4 +1770,141 @@ mod tests { |
| 1706 | eng.queue().submit(Some(enc.finish())); | 1770 | eng.queue().submit(Some(enc.finish())); |
| 1707 | assert_eq!(cache.cached_bind_group_count(), CUSTOM_BIND_GROUP_CACHE_CAPACITY); | 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 | } |