d3d12x.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //! `d3d12_xs.dll`: virtual memory setup, device creation, root signature
  2. //! serialization. The device and everything it creates live in
  3. //! `victory-gpu`.
  4. use std::ffi::c_void;
  5. use victory_gpu::device;
  6. use victory_gpu::object::{HRESULT, Iid, Object};
  7. use crate::{HleExport, HleModule};
  8. /// Device creation; first hands the device the title's general allocator
  9. /// (`XMemAlloc`, which honours the title's hooks) for resource memory.
  10. extern "system" fn create_device(
  11. adapter: *const c_void,
  12. parameters: *const u32,
  13. iid: *const Iid,
  14. out: *mut *const Object,
  15. ) -> HRESULT {
  16. victory_gpu::resource::use_allocator(super::xmem::XMemAlloc, super::xmem::XMemFree);
  17. victory_gpu::resource::use_page_mapper(super::xmem::map_alias);
  18. victory_gpu::resource::use_many_page_mapper(super::xmem::map_aliases);
  19. #[cfg(windows)]
  20. victory_gpu::on_failure(host_gpu_failed);
  21. device::create(adapter, parameters, iid, out)
  22. }
  23. /// A host GPU failure ends the run like a crash: reported to the crash log,
  24. /// then the process ends as the console ends a title.
  25. #[cfg(windows)]
  26. fn host_gpu_failed(what: &str) -> ! {
  27. use windows_sys::Win32::System::Threading::{GetCurrentProcess, TerminateProcess};
  28. victory_loader::log_crash(&format!("VIctory: host GPU failure: {what}"));
  29. const DXGI_ERROR_DEVICE_REMOVED: u32 = 0x887A_0005;
  30. // SAFETY: ends our own process.
  31. unsafe { TerminateProcess(GetCurrentProcess(), DXGI_ERROR_DEVICE_REMOVED) };
  32. unreachable!("the process has ended")
  33. }
  34. /// Translates `container` into the cache ([`use_shader_cache`]) ahead of
  35. /// any run: whether it was there already, or why it does not translate.
  36. pub fn precompile(container: &[u8]) -> Result<bool, String> {
  37. #[cfg(windows)]
  38. {
  39. victory_gpu::shader_cache::precompile(container)
  40. }
  41. #[cfg(not(windows))]
  42. {
  43. let _ = container;
  44. Err("no host".into())
  45. }
  46. }
  47. /// The fork's offline warm-up (`victory translate --warm`), by
  48. /// [`precompile`]: whether it was translated now.
  49. pub fn warm_shader(container: &[u8]) -> Result<bool, String> {
  50. #[cfg(windows)]
  51. {
  52. victory_gpu::shader_cache::precompile(container).map(|was_there| !was_there)
  53. }
  54. #[cfg(not(windows))]
  55. {
  56. let _ = container;
  57. Err("no shader cache on this host".into())
  58. }
  59. }
  60. /// Keeps the host's translations of the title's shaders in `dir` (a
  61. /// folder of the cache) across runs, and its compiled pipelines beside it
  62. /// (`pipelines`).
  63. pub fn use_shader_cache(dir: std::path::PathBuf) {
  64. #[cfg(windows)]
  65. {
  66. victory_gpu::pipeline::use_pipeline_cache(dir.with_file_name("pipelines"));
  67. victory_gpu::shader_cache::use_shader_cache(dir);
  68. }
  69. #[cfg(not(windows))]
  70. let _ = dir;
  71. }
  72. pub(crate) fn module() -> HleModule {
  73. HleModule {
  74. name: "d3d12x",
  75. dlls: &["d3d12_xs.dll"],
  76. exports: vec![
  77. HleExport {
  78. name: "D3D12ConfigureVirtualMemoryX",
  79. address: device::configure_virtual_memory as *const () as usize,
  80. },
  81. HleExport {
  82. name: "D3D12XboxCreateDevice",
  83. address: create_device as *const () as usize,
  84. },
  85. HleExport {
  86. name: "D3D12SerializeRootSignature",
  87. address: victory_gpu::root::serialize as *const () as usize,
  88. },
  89. HleExport {
  90. name: "D3D12SerializeVersionedRootSignature",
  91. address: victory_gpu::root::serialize_versioned as *const () as usize,
  92. },
  93. // The AMD upscaler's (Series S).
  94. HleExport {
  95. name: "D3D12CreateRootSignatureDeserializer",
  96. address: victory_gpu::root::create_deserializer as *const () as usize,
  97. },
  98. // A data import: the title reads the id at this address.
  99. HleExport {
  100. name: "IID_ID3D12Device",
  101. address: (&raw const device::IID) as usize,
  102. },
  103. ],
  104. }
  105. }
  106. #[cfg(test)]
  107. mod tests {
  108. use super::*;
  109. use crate::{HleResolver, HostPolicy};
  110. use victory_loader::ImportResolver;
  111. use victory_loader::Resolution;
  112. use victory_pe::ImportSymbol;
  113. #[test]
  114. fn the_interface_id_import_is_bound_to_data() {
  115. let symbol = ImportSymbol::Name {
  116. hint: 0,
  117. name: "IID_ID3D12Device".into(),
  118. };
  119. let r = HleResolver::new(HostPolicy::none());
  120. let Resolution::Found { address, .. } = r.resolve("d3d12_xs.dll", &symbol) else {
  121. panic!("not bound");
  122. };
  123. // SAFETY: the address is our static id.
  124. assert_eq!(unsafe { *(address as *const [u8; 16]) }, device::IID);
  125. }
  126. }