Repository navigation
GPU: emulate indirect mesh dispatches #49
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2078,6 +2078,7 @@ impl State { | |
| p: &HostPipeline, | ||
| buffers: &crate::mesh_emu::Buffers, | ||
| base_group: u32, | ||
| slot_map_gpu: u64, | ||
| ) -> bool { | ||
| let Some(emu) = &p.mesh_emu else { | ||
| return false; | ||
|
|
@@ -2104,10 +2105,11 @@ impl State { | |
| l.SetComputeRoot32BitConstants(param, 4, constants.as_ptr().cast(), 0); | ||
| } | ||
| if let Some(param) = emu.cs_srv_param { | ||
| l.SetComputeRootShaderResourceView(param, buffers.dummy_srv_gpu); | ||
| l.SetComputeRootShaderResourceView(param, slot_map_gpu); | ||
| } | ||
| } | ||
| list.keep(&buffers.output); | ||
| list.keep(&buffers.slot_map); | ||
| list.keep(&buffers.dummy_srv); | ||
| true | ||
| } | ||
|
|
@@ -2622,7 +2624,7 @@ impl State { | |
| let mut base = 0u32; | ||
| while base < x { | ||
| let chunk = (x - base).min(group_cap); | ||
| if !self.bind_emulated_cs(list, p, buffers, base) { | ||
| if !self.bind_emulated_cs(list, p, buffers, base, buffers.dummy_srv_gpu) { | ||
| self.restore_mesh_emu_state(list); | ||
| return false; | ||
| } | ||
|
|
@@ -3187,17 +3189,27 @@ impl State { | |
| }, | ||
| None => None, | ||
| }; | ||
| if kind == MESH && (mesh_test() == 1 || !mesh_kept(self.pipeline)) { | ||
| why("mesh draws are switched off (VICTORY_MESH_TEST=skip)"); | ||
| return false; | ||
| } | ||
| if kind == MESH && p.mesh_emu.is_some() { | ||
| return self.execute_indirect_mesh_emulated( | ||
| list, | ||
| p, | ||
| (stride, max), | ||
| (&args, arguments, len), | ||
| counter.as_ref().map(|(s, at)| (s, *at)), | ||
| &why, | ||
| ); | ||
| } | ||
| let Some(signature) = matches!(kind, DRAW | INDEXED | DISPATCH | MESH) | ||
| .then(|| host_signature(kind, stride)) | ||
| .flatten() | ||
| else { | ||
| why("no host signature"); | ||
| return false; | ||
| }; | ||
| if kind == MESH && (mesh_test() == 1 || !mesh_kept(self.pipeline)) { | ||
| why("mesh draws are switched off (VICTORY_MESH_TEST=skip)"); | ||
| return false; | ||
| } | ||
| // A window of records clamped in one pass (`windows`): for single | ||
| // records without a count buffer, drawn by shaders that store to no | ||
| // buffer. Anything else ends the window: it uses the scratch buffer | ||
|
|
@@ -3344,6 +3356,167 @@ impl State { | |
| } | ||
| true | ||
| } | ||
| fn execute_indirect_mesh_emulated( | ||
| &mut self, | ||
| list: &HostList, | ||
| p: &HostPipeline, | ||
| (stride, max): (u32, u32), | ||
| (args, arguments, len): (&std::sync::Arc<crate::shadow::Shadow>, u64, u64), | ||
| counter: Option<(&std::sync::Arc<crate::shadow::Shadow>, u64)>, | ||
| why: &dyn Fn(&str), | ||
| ) -> bool { | ||
| if stride < 12 { | ||
| why("indirect mesh stride smaller than 12 bytes"); | ||
| return false; | ||
| } | ||
| if max > crate::mesh_emu::MAX_INDIRECT_RECORDS { | ||
| why("indirect mesh record count exceeds the emulation safety limit"); | ||
| return false; | ||
| } | ||
| let Some(emu_pipe) = &p.mesh_emu else { | ||
| return false; | ||
| }; | ||
| let Some(buffers) = crate::mesh_emu::buffers() else { | ||
| why("no mesh emulation buffers"); | ||
| return false; | ||
| }; | ||
| let group_cap = buffers.group_cap(emu_pipe.emu.group_bytes()); | ||
| if group_cap == 0 { | ||
| why("mesh emulation buffer smaller than one group"); | ||
| return false; | ||
| } | ||
| let (Some(dispatch_sig), Some(draw_sig)) = (host_signature(2, 12), host_signature(0, 16)) | ||
| else { | ||
| why("no host indirect command signature"); | ||
| return false; | ||
| }; | ||
| let count_gpu = counter.map(|(s, at)| s.gpu(at)); | ||
| if count_gpu.is_some_and(|g| !g.is_multiple_of(4)) { | ||
| why("count not 4-byte aligned"); | ||
| return false; | ||
| } | ||
| args.refresh(list, arguments, len); | ||
| let count_arg = counter.as_ref().map(|(s, at)| { | ||
| s.refresh(list, *at, 4); | ||
| (&s.buffer, s.offset(*at)) | ||
| }); | ||
| // Clamp the title's records before the preparation pass reads them; | ||
| // this also applies the shared indirect work budget. | ||
| let Some(bounded) = crate::indirect::bound( | ||
| list, | ||
| (&args.buffer, args.offset(arguments)), | ||
| count_arg, | ||
| (10, stride, max), | ||
| ) else { | ||
| why("not bounded (no clamp, or arguments not 4-byte aligned)"); | ||
| return false; | ||
| }; | ||
| // SAFETY: `bounded` is the live scratch resource returned by `bound`. | ||
| let bounded_gpu = unsafe { bounded.GetGPUVirtualAddress() }; | ||
| if !buffers.prepare_indirect( | ||
| list, | ||
| &bounded, | ||
| bounded_gpu, | ||
| count_gpu, | ||
| stride, | ||
| max, | ||
| emu_pipe.emu.max_primitives, | ||
| group_cap, | ||
| ) { | ||
| crate::indirect::done(list, &bounded); | ||
| why("indirect mesh preparation inputs exceed safe limits"); | ||
| return false; | ||
| } | ||
| list.keep(&buffers.indirect_args); | ||
| self.host_pipeline = 0; | ||
| self.host_roots = [0; 2]; | ||
|
|
||
| let finish_indirect = |list: &HostList| { | ||
| let args_back = transition( | ||
| &buffers.indirect_args, | ||
| D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, | ||
| D3D12_RESOURCE_STATE_UNORDERED_ACCESS, | ||
| ); | ||
| // SAFETY: an open list; emulation buffers return to UAV for the | ||
| // next preparation pass. | ||
| unsafe { | ||
| if max > 1 { | ||
| let map_back = transition( | ||
| &buffers.slot_map, | ||
| D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, | ||
| D3D12_RESOURCE_STATE_UNORDERED_ACCESS, | ||
| ); | ||
| list.list.ResourceBarrier(&[args_back, map_back]); | ||
| } else { | ||
| list.list.ResourceBarrier(&[args_back]); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let (base_group, slot_map_gpu) = if max > 1 { | ||
| ( | ||
| victory_shader::emit::INDIRECT_SLOT_MAP_FLAG, | ||
| buffers.slot_map_gpu, | ||
| ) | ||
| } else { | ||
| (0, buffers.dummy_srv_gpu) | ||
| }; | ||
| if !self.bind_emulated_cs(list, p, buffers, base_group, slot_map_gpu) { | ||
| finish_indirect(list); | ||
| self.restore_mesh_emu_state(list); | ||
| why("prepare emulated mesh CS failed"); | ||
| return false; | ||
| } | ||
| #[cfg(feature = "diagnostics")] | ||
| list.note_op(|| { | ||
| self.op_text( | ||
| format!("indirect mesh-emu max {max} at {arguments:#x}"), | ||
| false, | ||
| ) | ||
| }); | ||
| // SAFETY: open list with the emulated compute pipeline bound, | ||
| // generated indirect arguments, and `output` in UAV state. | ||
| unsafe { | ||
| list.list | ||
| .ExecuteIndirect(&dispatch_sig, 1, &buffers.indirect_args, 0, None, 0); | ||
| list.list.ResourceBarrier(&[transition( | ||
| &buffers.output, | ||
| D3D12_RESOURCE_STATE_UNORDERED_ACCESS, | ||
| D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, | ||
| )]); | ||
|
Comment on lines
+3481
to
+3486
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: A emulação registra as barreiras antes de consumir os buffers A limitação dos argumentos e a preparação do despacho registram suas próprias barreiras. O despacho computacional também transiciona a saída antes de o shader de vértices lê-la. Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
| if !self.bind_emulated_gfx(list, p, buffers) { | ||
| // SAFETY: restore `output` for the next dispatch. | ||
| unsafe { | ||
| list.list.ResourceBarrier(&[transition( | ||
| &buffers.output, | ||
| D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, | ||
| D3D12_RESOURCE_STATE_UNORDERED_ACCESS, | ||
| )]); | ||
| } | ||
| finish_indirect(list); | ||
| self.restore_mesh_emu_state(list); | ||
| why("prepare emulated mesh graphics failed"); | ||
| return false; | ||
| } | ||
| self.mesh_scissor(list, true); | ||
| // SAFETY: open list with the unpack graphics pipeline bound and | ||
| // generated draw arguments at offset 16. | ||
| unsafe { | ||
| list.list | ||
| .ExecuteIndirect(&draw_sig, 1, &buffers.indirect_args, 16, None, 0); | ||
| list.list.ResourceBarrier(&[transition( | ||
| &buffers.output, | ||
| D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, | ||
| D3D12_RESOURCE_STATE_UNORDERED_ACCESS, | ||
| )]); | ||
| } | ||
| self.mesh_scissor(list, false); | ||
| finish_indirect(list); | ||
| self.restore_mesh_emu_state(list); | ||
| DRAWN.fetch_add(1, Ordering::Relaxed); | ||
| true | ||
| } | ||
| } | ||
|
|
||
| /// The host command signature for one argument of `kind` with records | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Desenhos indiretos reutilizam argumentos sobrescritos pela emulação de mesh
Com
VICTORY_INDIRECT_WINDOW=1, um despacho de mesh emulado sobrescreve o buffer temporário sem invalidar os argumentos em cache. O próximo desenho da mesma lista que reutilizar esses argumentos lê contagens do despacho de mesh no lugar delas.Learn more
Com a otimização ativada, execute_indirect guarda argumentos já limitados para que desenhos seguintes possam lê-los sem repetir a etapa de limitação. A janela guarda uma referência ao buffer temporário da lista em vez de copiar os argumentos (criação da janela). O desvio para emulação pula a invalidação existente, mas o limitador usa o mesmo buffer temporário quando a capacidade é suficiente. Ao retornar a um registro coberto pela janela na mesma lista, o caminho de desenho lê o buffer sobrescrito.
Example: Uma lista desenha um triângulo indiretamente e mantém o registro em cache. Depois, ela emula um despacho indireto com grupos
(8, 1, 1)e repete o desenho original. O desenho repetido lê oito vértices com início no vértice um, em vez dos três vértices a partir do zero.Recommended fix: Limpe
self.windowantes de chamarindirect::boundno caminho emulado. O desenho seguinte volta a limitar os argumentos a partir do buffer original do jogo.Was this helpful? React with 👍 or 👎 to provide feedback.